· 8 years ago · Jul 27, 2018, 10:46 PM
1<?php
2global $ArcaneWpTeamWars;
3
4define('ARCANE_TEAMWARS_CATEGORY', '_wp_teamwars_category');
5define('ARCANE_TEAMWARS_DEFAULTCSS', '_wp_teamwars_defaultcss');
6define('ARCANE_TEAMWARS_ACL', '_wp_teamwars_acl');
7require_once(ABSPATH . 'wp-admin/includes/screen.php');
8
9require ( get_parent_theme_file_path('addons/team-wars/wp-teamwars-widget.php'));
10require ( get_parent_theme_file_path('addons/team-wars/wp-upcoming-matches-widget.php'));
11require ( get_parent_theme_file_path('addons/team-wars/wp-other-matches-widget.php'));
12
13class Arcane_TeamWars {
14
15 var $tables = array(
16 'games' => 'cw_games',
17 'maps' => 'cw_maps',
18 );
19
20 var $match_status = array();
21 var $acl_keys = array();
22 var $page_hooks = array();
23 var $page_notices = array();
24
25 const ErrorOK = 0;
26 const ErrorDatabase = -199;
27 const ErrorUploadMaxFileSize = -208;
28 const ErrorUploadHTMLMaxFileSize = -209;
29 const ErrorUploadPartially = -210;
30 const ErrorUploadNoFile = -211;
31 const ErrorUploadMissingTemp = -212;
32 const ErrorUploadDiskWrite = -213;
33 const ErrorUploadStoppedByExt = -214;
34 const ErrorUploadFileTypeNotAllowed = -215;
35
36 function __construct() {
37 $this->tables = array_map(create_function('$t', 'global $table_prefix; return $table_prefix . $t; '), $this->tables);
38
39 add_action('widgets_init', array($this, 'on_widgets_init'));
40 }
41
42
43 /**
44 * WP init hook
45 *
46 * Plugin initialization method used to load textdomain,
47 * register hooks, scripts and styles.
48 *
49 * @return void
50 */
51
52 function on_init()
53 {
54
55
56 add_option(ARCANE_TEAMWARS_CATEGORY, -1);
57 add_option(ARCANE_TEAMWARS_DEFAULTCSS, true);
58 add_option(ARCANE_TEAMWARS_ACL, array());
59
60 // update database
61
62
63 $this->acl_keys = array(
64 'manage_matches' => esc_html__('Manage matches', 'arcane'),
65 'manage_games' => esc_html__('Manage games', 'arcane'),
66 'manage_teams' => esc_html__('Manage teams', 'arcane')
67 );
68
69 $this->match_status = array(
70 esc_html__('Friendly', 'arcane'),
71 esc_html__('Official', 'arcane')
72 );
73
74
75
76
77 add_action('admin_post_wp-teamwars-deleteteams', array($this, 'on_admin_post_deleteteams'));
78 add_action('admin_post_wp-teamwars-sethometeam', array($this, 'on_admin_post_sethometeam'));
79 add_action('admin_post_wp-teamwars-gamesop', array($this, 'on_admin_post_gamesop'));
80 add_action('admin_post_wp-teamwars-deletemaps', array($this, 'on_admin_post_deletemaps'));
81 add_action('admin_post_wp-teamwars-deletematches', array($this, 'on_admin_post_deletematches'));
82
83 add_action('admin_post_wp-teamwars-settings', array($this, 'on_admin_post_settings'));
84 add_action('admin_post_wp-teamwars-acl', array($this, 'on_admin_post_acl'));
85 add_action('admin_post_wp-teamwars-deleteacl', array($this, 'on_admin_post_deleteacl'));
86 add_action('admin_post_wp-teamwars-import', array($this, 'on_admin_post_import'));
87
88 add_action('wp_ajax_get_maps', array($this, 'on_ajax_get_maps'));
89
90 }
91
92 /**
93 * WP admin_menu hook
94 *
95 * Page, Assets registration, load-* action hooks
96 *
97 * @return void
98 */
99
100
101 function on_tournaments () {
102 header('Location: '.get_admin_url().'edit.php?post_type=tournament');
103 }
104
105 function on_teams_redirect() {
106 header('Location: '.get_admin_url().'edit.php?post_type=team');
107 }
108 function acl_user_can($action, $value = false, $user_id = false)
109 {
110 global $user_ID;
111
112 $acl = $this->acl_get();
113 $is_super = false;
114 $caps = array(
115 'games' => array(),
116 'permissions' => array_fill_keys(array_keys($this->acl_keys), false)
117 );
118
119 if(empty($user_id))
120 $user_id = $user_ID;
121
122 if(!empty($acl) && isset($acl[$user_id]))
123 $caps = $acl[$user_id];
124
125 $user = new WP_User($user_id);
126 if(!empty($user))
127 $is_super = $user->has_cap('manage_options');
128
129 if($is_super) {
130 $caps['games'] = array('all');
131 array_walk($caps['permissions'], create_function('&$v, &$k', '$v = true;'));
132 }
133
134 switch($action)
135 {
136 case 'which_games':
137
138 $where = array_search(0, $caps['games']);
139
140 if($where === false)
141 return $caps['games'];
142
143 return 'all';
144
145 break;
146
147 case 'manage_game':
148
149 if($value == 'all')
150 $value = 0;
151
152 $ret = array_search($value, $caps['games']) !== false;
153
154 if(!$ret) {
155 $ret = array_search(0, $caps['games']) !== false;
156 }
157
158 return $ret;
159
160 break;
161 }
162
163 return isset($caps['permissions'][$action]) && $caps['permissions'][$action];
164 }
165
166 function acl_get() {
167 $acl = get_option(ARCANE_TEAMWARS_ACL);
168
169 if(!is_array($acl))
170 $acl = array();
171
172 return $acl;
173 }
174
175 function acl_update($user_id, $data) {
176
177 $acl = $this->acl_get();
178
179 $acl[$user_id] = array(
180 'games' => array(0),
181 'permissions' => array('manage_matches')
182 );
183
184 $default_perms = array(
185 'manage_matches' => false,
186 'manage_teams' => false,
187 'manage_games' => false
188 );
189
190 $acl[$user_id]['games'] = isset($data['games']) ? array_unique(array_values($data['games'])) : array(0);
191 $acl[$user_id]['permissions'] = isset($data['permissions']) ? $this->extract_args($data['permissions'], $default_perms) : $default_perms;
192
193 update_option(ARCANE_TEAMWARS_ACL, $acl);
194
195 return true;
196 }
197
198 function acl_delete($user_id) {
199
200 $acl = $this->acl_get();
201
202 if(isset($acl[$user_id])) {
203 unset($acl[$user_id]);
204 update_option(ARCANE_TEAMWARS_ACL, $acl);
205
206 return true;
207 }
208
209 return false;
210 }
211
212
213 function on_widgets_init()
214 {
215 register_widget('Arcane_TeamWars_Widget');
216 register_widget('Arcane_Upcoming_Matches_Widget');
217 register_widget('Arcane_Other_Matches_Widget');
218 return;
219
220 }
221
222
223
224
225
226 function html_notice_helper($message, $type = 'updated', $echo = true) {
227 $arcane_allowed = wp_kses_allowed_html( 'post' );
228 $text = '<div class="' . $type . ' fade"><p>' . $message . '</p></div>';
229
230 if($echo) echo wp_kses($text,$arcane_allowed);
231
232 return $text;
233 }
234
235 function print_table_header($columns, $id = true)
236 {
237
238 $arcane_allowed = wp_kses_allowed_html( 'entities ' );
239 foreach ( $columns as $column_key => $column_display_name ) {
240 $class = ' class="manage-column';
241
242 $class .= " column-$column_key";
243
244 if ( 'cb' == $column_key )
245 $class .= ' check-column';
246 elseif ( in_array($column_key, array('posts', 'comments', 'links')) )
247 $class .= ' num';
248
249 $class .= '"';
250 ?>
251 <th scope="col" <?php echo esc_attr($id) ? "id=\"$column_key\"" : ""; echo wp_kses($class,$arcane_allowed); ?>><?php echo wp_kses ($column_display_name,
252 array(
253
254 'input' => array(
255 'type' => array()
256 ),
257 )
258 ); ?></th>
259 <?php }
260 }
261
262 function add_notice($message, $type = 'updated') {
263
264 if(empty($type)) $type = 'updated';
265
266 if(!isset($this->page_notices[$type])) {
267 $this->page_notices[$type] = array();
268 }
269
270 $this->page_notices[$type][] = $message;
271 }
272
273 function print_notices() {
274 foreach($this->page_notices as $type => $e) {
275 foreach($e as $msg) {
276 $this->html_notice_helper($msg, $type, true);
277 }
278 }
279 }
280
281 /**
282 * Image uploading handling, used internally by plugin
283 *
284 * @param string $name $_FILES array key for a file which should be uploaded
285 *
286 * @return void
287 */
288
289 function handle_upload($name)
290 {
291 $mimes = apply_filters('upload_mimes',
292 array('jpg|jpeg|jpe' => 'image/jpeg',
293 'gif' => 'image/gif',
294 'png' => 'image/png'));
295
296 $upload = isset($_FILES[$name]) ? $_FILES[$name] : false;
297 $upload_errors = array(self::ErrorOK,
298 self::ErrorUploadMaxFileSize,
299 self::ErrorUploadHTMLMaxFileSize,
300 self::ErrorUploadPartially,
301 self::ErrorUploadNoFile,
302 self::ErrorOK,
303 self::ErrorUploadMissingTemp,
304 self::ErrorUploadDiskWrite,
305 self::ErrorUploadStoppedByExt);
306
307 if(!empty($upload))
308 {
309 if($upload['error'] > 0)
310 return $upload_errors[$upload['error']];
311
312 extract(wp_check_filetype($upload['name'], $mimes));
313
314 if(!$type || !$ext)
315 {
316 return self::ErrorUploadFileTypeNotAllowed;
317 }
318 else
319 {
320 $file_data = wp_handle_upload($upload,
321 array('test_type' => false,
322 'test_form' => false,
323 'upload_error_handler' =>
324 create_function('&$file, $message',
325 '$code = $file["error"];
326 $errors = array(' . implode(', ', $upload_errors) . ');
327 if(isset($errors[$code]))
328 return $errors[$code];
329 return $errors[0];')));
330
331 if(!empty($file_data) && is_array($file_data))
332 {
333 $file_data['type'] = $type;
334 if(!isset($file_data['error']))
335 {
336 $fileinfo = pathinfo($file_data['file']);
337 $attach_title = basename($fileinfo['basename'], '.' . $fileinfo['extension']);
338 $attach_id = wp_insert_attachment(array('guid' => $file_data['url'],
339 'post_title' => $attach_title,
340 'post_content' => '',
341 'post_status' => 'publish',
342 'post_mime_type' => $file_data['type']),
343 $file_data['file']);
344
345 $metadata = wp_generate_attachment_metadata($attach_id, $file_data['file']);
346
347 if(!empty($metadata))
348 wp_update_attachment_metadata($attach_id, $metadata);
349
350 if(!empty($attach_id) && is_int($attach_id))
351 return $attach_id;
352 } else {
353 return $upload_errors[$file_data['error']];
354 }
355 }
356 }
357 }
358
359 return self::ErrorOK;
360 }
361
362 /**
363 * Parse arguments and return a list of values with keys from defaults
364 *
365 * @param array|string $args Input values
366 * @param array $defaults Array of default values
367 * @return array Merged array. Same behaviour as wp_parse_args except it generates array which only consists of keys from $defaults array
368 */
369
370 function extract_args($args, $defaults) {
371 $rslt = array();
372
373 $options = wp_parse_args($args, $defaults);
374
375 if(is_array($defaults))
376 foreach(array_keys($defaults) as $key)
377 $rslt[$key] = $options[$key];
378
379 return $rslt;
380 }
381
382
383
384
385 function get_team($p, $count = false)
386 {
387 //UPDATED TO POSTS
388 extract($this->extract_args($p, array(
389 'id' => false,
390 'title' => false,
391 'limit' => 0,
392 'offset' => 0,
393 'orderby' => 'id',
394 'order' => 'ASC')));
395
396 $order = strtoupper($order);
397 if($order != 'ASC' && $order != 'DESC')
398 $order = 'ASC';
399
400 if(($id != 'all' && $id !== false) OR ((is_array($id)) AND (!empty($id)))) {
401 if (is_array($id)) {
402 $returns = array();
403 $counter = 0;
404 foreach ($id as $single) {
405 $returns[$counter] = (object) array_merge((array) get_post($single), (array) arcane_get_meta($single));
406 $returns[$counter]->title = $returns[$counter]->post_title;
407 $returns[$counter]->id = $returns[$counter]->ID;
408 $counter++;
409 }
410 return $returns;
411 } else {
412 $obj_merged = (object) array_merge((array) get_post($id), (array) arcane_get_meta($id));
413 if(isset($obj_merged->post_title))
414 $obj_merged->title = $obj_merged->post_title;
415 if(isset($obj_merged->ID))
416 $obj_merged->id = $obj_merged->ID;
417 return $obj_merged;
418 }
419 } else {
420 //id not set
421 if ($limit == 0) {
422 $limit = -1;
423 }
424 $args = array(
425 'post_type' => 'team',
426 'posts_per_page' => $limit,
427 'offset' => $offset,
428 'post_status' => 'publish',
429 'orderby' => $orderby,
430 'order' => $order
431 );
432
433 if($title !== false) {
434 $test = get_page_by_title($title, OBJECT, 'post');
435 if ($test) {
436 return $test;
437 } else {
438 return false;
439 }
440 }
441 $posts = get_posts ($args);
442 $returner = array();
443 if (is_array($posts)) {
444 foreach ($posts as $post) {
445 $obj_merged = (object) array_merge((array) $post, (array) arcane_get_meta($post->ID));
446 $obj_merged->title = $obj_merged->post_title;
447 $obj_merged->id = $obj_merged->ID;
448 $returner[] = $obj_merged;
449 }
450 }
451 return $returner;
452 }
453 }
454
455 function add_team($p)
456 {
457 //UPDATED TO POSTS
458 $data = $this->extract_args($p, array(
459 'title' => '',
460 'logo' => 0,
461 'home_team' => 0,
462 'post_id' => ''));
463 if ( FALSE === get_post_status( $data['post_id'] ) ) {
464 //team doesn't exist create it
465 $args = array(
466 'post_type' => 'team',
467 'post_title' => $data['title'],
468 'post_status'=> 'publish',
469 'post_content' => '',
470 'post_name' => sanitize_title($data['title'])
471 );
472 $pid = wp_insert_post($args);
473 if ($pid) {
474 update_post_meta($pid, 'team_photo', $data['logo']);
475 update_post_meta($pid, 'home_team', $data['home_team']);
476 return true;
477 } else {
478 return false;
479 }
480 } else {
481 //team exists update it
482 $args = array(
483 'ID' => $data['post_id'],
484 'post_title' => $data['title'],
485 'post_name' => sanitize_title($data['title'])
486 );
487 wp_update_post( $args );
488 update_post_meta($data['post_id'], 'team_photo', $data['logo']);
489 update_post_meta($data['post_id'], 'home_team', $data['home_team']);
490 return $data['post_id'];
491 }
492 }
493
494 function set_hometeam($id) {
495 //UPDATED TO POSTS
496 return update_post_meta($id, 'home_team', 1);
497 }
498
499 function update_team($id, $p)
500 {
501 //UPDATED TO POSTS
502 $data = wp_parse_args($p, array());
503 if ( FALSE === get_post_status( $id ) ) {
504 //team doesnt exist, create instead, dunno how tho
505 $this->add_team($p);
506 } else {
507 //team exists
508
509 //update title
510 if (isset($data['title']) AND (strlen($data['title']) > 1)) {
511 $args = array(
512 'ID' => $id,
513 'post_title' => $data['title']
514 );
515 wp_update_post( $args );
516 }
517 //update home_team
518 if (isset($data['home_team']) AND (strlen($data['home_team']) > 1)) {
519 update_post_meta($id, 'home_team', $data['home_team']);
520 }
521 if (isset($data['logo']) AND (strlen($data['logo']) > 1)) {
522 update_post_meta($id, 'team_photo', $data['logo']);
523 }
524 }
525
526 return $id;
527 }
528
529 function delete_team($id, $skipdelete = false)
530 {
531 //UPDATED TO POSTS
532 if(!is_array($id))
533 $id = array($id);
534
535 $id = array_map('intval', $id);
536
537 // delete matches belongs to this team
538 $this->delete_match_by_team($id);
539 //parse ids, remove post
540 if (is_array($id)) {
541 foreach($id as $small_id) {
542 if ($small_id > 0) {
543 if ($skipdelete == false) {
544 wp_delete_post($small_id);
545 }
546 }
547 }
548 } else {
549 if (is_int($id)) {
550 if ($id > 0) {
551 if ($skipdelete == false) {
552 wp_delete_post($id);
553 }
554 }
555 }
556 }
557 return true;
558 }
559
560 function on_admin_post_deleteteams()
561 {
562 //UPDATED TO POSTS
563 if(!$this->acl_user_can('manage_teams'))
564 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
565
566 check_admin_referer('wp-teamwars-deleteteams');
567
568 $referer = remove_query_arg(array('add', 'update'), $_REQUEST['_wp_http_referer']);
569
570 if($_REQUEST['do_action'] == 'delete' || $_REQUEST['do_action2'] == 'delete') {
571 extract($this->extract_args($_REQUEST, array('delete' => array())));
572
573 $error = $this->delete_team($delete);
574 $referer = add_query_arg('delete', $error, $referer);
575 }
576
577 wp_redirect($referer, $status = 302);
578 }
579
580 function on_admin_post_sethometeam()
581 {
582 //UPDATED TO POSTS
583 if(!$this->acl_user_can('manage_teams'))
584 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
585
586 check_admin_referer('wp-teamwars-sethometeam');
587
588 $referer = $_REQUEST['_wp_http_referer'];
589
590 extract($this->extract_args($_REQUEST, array('id' => array())));
591
592 $error = $this->set_hometeam($id);
593
594 wp_redirect($referer, $status = 302);
595 }
596
597 function escape_array($arr){
598 global $wpdb;
599 $escaped = array();
600 foreach($arr as $k => $v){
601 if(is_numeric($v))
602 $escaped[] = $wpdb->prepare('%d', $v);
603 else
604 $escaped[] = $wpdb->prepare('%s', $v);
605 }
606 return implode(',', $escaped);
607 }
608
609 /*
610 * Games Managment
611 */
612
613 function get_game($p, $count = false)
614 {
615 global $wpdb;
616
617 extract($this->extract_args($p, array(
618 'id' => false,
619 'limit' => 0,
620 'offset' => 0,
621 'orderby' => 'id',
622 'order' => 'ASC')));
623
624 $limit_query = '';
625 $order_query = '';
626 $where_query = '';
627
628 $order = strtolower($order);
629 if($order != 'asc' && $order != 'desc')
630 $order = 'asc';
631
632 $order_query = $wpdb->prepare('ORDER BY %s %s', $orderby, $order );
633
634 if($id != 'all' && $id !== false) {
635
636 if(!is_array($id))
637 $id = array($id);
638
639 $id = array_map('intval', $id);
640 //$id = implode(', ', $id);
641 $where_query = array();
642 $where_query[] = "id IN (" . $this->escape_array($id) . ")";
643 }
644
645 if($limit > 0) {
646 $limit_query = $wpdb->prepare('LIMIT %d, %d', $offset, $limit);
647 }
648
649
650 if(!empty($where_query))
651 $where_query = 'WHERE ' . implode(' AND ', $where_query);
652
653 if($count) {
654
655 $rslt = $wpdb->get_row('SELECT COUNT(id) AS m_count FROM `' . $this->tables['games'] . '` ' . $where_query);
656
657 $ret = array('total_items' => 0, 'total_pages' => 1);
658
659 $ret['total_items'] = $rslt->m_count;
660
661 if($limit > 0)
662 $ret['total_pages'] = ceil($ret['total_items'] / $limit);
663
664 return $ret;
665 }
666
667 if(!isset($where_query))$where_query = '';
668
669 if (is_plugin_active( 'arcane_custom_post_types/arcane_custom_post_types.php' )){
670 $rslt = $wpdb->get_results('SELECT * FROM `' . $this->tables['games'] . '` ' . implode(' ', array($where_query, $order_query, $limit_query)));
671 }else{
672 $rslt = '';
673 }
674 return $rslt;
675 }
676
677 function get_games($p, $count = false)
678 {
679 global $wpdb;
680
681 extract($this->extract_args($p, array(
682 'id' => false,
683 'limit' => 0,
684 'offset' => 0,
685 'orderby' => 'id',
686 'order' => 'ASC')));
687
688 $limit_query = '';
689 $order_query = '';
690
691 $order = strtolower($order);
692 if($order != 'asc' && $order != 'desc')
693 $order = 'asc';
694
695 $order_query = 'ORDER BY `' . $orderby . '` ' . $order;
696
697 if($id != 'all' && $id !== false && !empty($id)) {
698 $where_query = array();
699 $where_query[] = 'id IN ('. $id . ')';
700 }
701
702 if($limit > 0) {
703 $limit_query = $wpdb->prepare('LIMIT %d, %d', $offset, $limit);
704 }
705
706
707 if(!empty($where_query))
708 $where_query = 'WHERE ' . implode(' AND ', $where_query);
709
710 if($count) {
711
712 $rslt = $wpdb->get_row('SELECT COUNT(id) AS m_count FROM `' . $this->tables['games'] . '` ' . $where_query);
713
714 $ret = array('total_items' => 0, 'total_pages' => 1);
715
716 $ret['total_items'] = $rslt->m_count;
717
718 if($limit > 0)
719 $ret['total_pages'] = ceil($ret['total_items'] / $limit);
720
721 return $ret;
722 }
723
724 if(!isset($where_query))$where_query = '';
725
726 if (is_plugin_active( 'arcane_custom_post_types/arcane_custom_post_types.php' )){
727 $rslt = $wpdb->get_results('SELECT * FROM `' . $this->tables['games'] . '` ' . implode(' ', array($where_query, $order_query, $limit_query)));
728 }else{
729 $rslt = '';
730 }
731
732 return $rslt;
733 }
734
735 function add_game($p)
736 {
737 global $wpdb;
738
739 $data = $this->extract_args($p, array('title' => '', 'abbr' => '', 'icon' => 0, 'g_banner_file' => 0));
740
741 if($wpdb->insert($this->tables['games'], $data, array('%s', '%s', '%d', '%d')))
742 {
743 $insert_id = $wpdb->insert_id;
744
745 return $insert_id;
746 }
747
748 return false;
749 }
750
751 function update_game($id, $p)
752 {
753 global $wpdb;
754
755 $fields = array('title' => '%s', 'abbr' => '%s', 'icon' => '%d', 'g_banner_file' => '%d');
756
757 $data = wp_parse_args($p, array());
758
759 $update_data = array();
760 $update_mask = array();
761
762 foreach($fields as $fld => $mask) {
763 if(isset($data[$fld])) {
764 $update_data[$fld] = $data[$fld];
765 $update_mask[] = $mask;
766 }
767 }
768
769 return $wpdb->update($this->tables['games'], $update_data, array('id' => $id), $update_mask, array('%d'));
770 }
771
772 function delete_game($id)
773 {
774 global $wpdb;
775
776 if(!is_array($id))
777 $id = array($id);
778
779 $id = array_map('intval', $id);
780
781 $this->delete_map_by_game($id);
782 $this->delete_match_by_game($id);
783
784 return $wpdb->query('DELETE FROM `' . $this->tables['games'] . '` WHERE id IN(' . implode(',', $id) . ')');
785 }
786
787
788 function on_admin_post_gamesop()
789 {
790 $arcane_allowed = wp_kses_allowed_html( 'post' );
791 if(!$this->acl_user_can('manage_games'))
792 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
793
794 check_admin_referer('wp-teamwars-gamesop');
795
796 $referer = remove_query_arg(array('add', 'update', 'export'), $_REQUEST['_wp_http_referer']);
797
798 $args = $this->extract_args($_REQUEST, array('do_action' => '', 'do_action2' => '', 'items' => array()));
799 extract($args);
800
801 $action = !empty($do_action) ? $do_action : (!empty($do_action2) ? $do_action2 : '');
802
803 if(!empty($items)) {
804
805 switch($action) {
806 case 'delete':
807 $error = $this->delete_game($items);
808 $referer = add_query_arg('delete', $error, $referer);
809 break;
810 case 'export':
811
812 $data = $this->export_games($items);
813
814 header('Content-Type: application/x-gzip-compressed');
815 header('Content-Disposition: attachment; filename="wp-teamwars-gamepack-' . date('Y-m-d', $this->current_time_fixed('timestamp')) . '.gz"');
816
817 $json = json_encode($data);
818
819 $gzdata = gzcompress($json, 9);
820
821 header('Content-Length: ' . strlen($gzdata));
822
823 echo $gzdata;
824
825 die();
826
827 break;
828 }
829
830 }
831
832 wp_redirect($referer, $status = 302);
833 }
834
835 function export_games($id)
836 {
837 $data = array();
838 $games = $this->get_game(array('id' => $id));
839
840 foreach($games as $game) {
841 $game_data = $this->extract_args($game, array(
842 'title' => '', 'abbr' => '',
843 'icon' => '', 'g_banner_file' => '', 'maplist' => array()
844 ));
845
846 $maplist = $this->get_map(array('game_id' => $game->id));
847
848 if($game->icon != 0) {
849 $attach = get_attached_file($game->icon);
850 $mimetype = get_post_mime_type($game->icon);
851 $pathinfo = pathinfo($attach);
852
853 if(!empty($attach)){
854 $content = $this->_get_file_content ($attach);
855
856 if(!empty($content))
857 $game_data['icon'] = array(
858 'filename' => $pathinfo['basename'],
859 'mimetype' => $mimetype,
860 'data' => $content);
861 }
862 }
863
864 if($game->g_banner_file != 0) {
865 $attach = get_attached_file($game->g_banner_file);
866 $mimetype = get_post_mime_type($game->g_banner_file);
867 $pathinfo = pathinfo($attach);
868
869 if(!empty($attach)){
870 $content = $this->_get_file_content ($attach);
871
872 if(!empty($content))
873
874 $game_data['g_banner_file'] = array(
875 'filename' => $pathinfo['basename'],
876 'mimetype' => $mimetype,
877 'data' => $content);
878 }
879 }
880
881 foreach($maplist as $map) {
882 $map_data = array('title' => $map->title, 'screenshot' => '');
883
884 if($map->screenshot != 0) {
885 $attach = get_attached_file($map->screenshot);
886 $mimetype = get_post_mime_type($map->screenshot);
887 $pathinfo = pathinfo($attach);
888
889 if(!empty($attach)){
890 $content = $this->_get_file_content ($attach);
891
892 if(!empty($content))
893 $map_data['screenshot'] = array(
894 'filename' => $pathinfo['basename'],
895 'mimetype' => $mimetype,
896 'data' => $content);
897 }
898 }
899
900 $game_data['maplist'][] = $map_data;
901 }
902
903 $data[] = $game_data;
904 }
905
906 return $data;
907 }
908
909 function _import_image($p) {
910
911 if(!empty($p)) {
912 $upload = wp_upload_bits($p['filename'], null, base64_decode($p['data']));
913
914 if($upload['error'] === false) {
915 $attach = array('guid' => $upload['url'],
916 'post_title' => sanitize_title($p['filename']),
917 'post_content' => '',
918 'post_status' => 'publish',
919 'post_mime_type' => $p['mimetype']);
920
921 $attach_id = wp_insert_attachment($attach, $upload['file']);
922
923 if(!empty($attach_id)) {
924 $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
925
926 if(!empty($metadata))
927 wp_update_attachment_metadata($attach_id, $metadata);
928
929 return $attach_id;
930 }
931 }
932 }
933
934 return 0;
935 }
936
937 function import_games($data) {
938
939 if(is_string($data))
940 $data = json_decode(gzuncompress($data));
941
942 if(empty($data) || !is_array($data))
943 return false;
944
945 foreach($data as $game) {
946
947 $game_data = $this->extract_args($game, array(
948 'title' => '', 'abbr' => '',
949 'icon' => '', 'g_banner_file' => '', 'maplist' => array()
950 ));
951
952 if(!empty($game_data['title'])) {
953 $p = $game_data;
954 $p['icon'] = $this->_import_image((array)$p['icon']);
955 $p['g_banner_file'] = $this->_import_image((array)$p['g_banner_file']);
956 $maplist = $p['maplist'];
957
958 unset($p['maplist']);
959
960 $game_id = $this->add_game($p);
961 if(!empty($game_id)) {
962
963 foreach($maplist as $map) {
964 $p = (array)$map;
965 $p['screenshot'] = $this->_import_image((array)$p['screenshot']);
966 $p['game_id'] = $game_id;
967
968 if(!empty($p['title']))
969 $this->add_map($p);
970 }
971
972 }
973 }
974
975 }
976
977 return true;
978 }
979
980 function on_add_game()
981 {
982 return $this->game_editor(esc_html__('New Game', 'arcane'), 'wp-teamwars-addgame', esc_html__('Add Game', 'arcane'));
983 }
984
985 function on_edit_game()
986 {
987
988 $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
989
990 return $this->game_editor(esc_html__('Edit Game', 'arcane'), 'wp-teamwars-editgame', esc_html__('Update Game', 'arcane'), $id);
991 }
992
993 function on_load_manage_games()
994 {
995 $act = isset($_GET['act']) ? $_GET['act'] : '';
996 $id = isset($_GET['id']) ? $_GET['id'] : 0;
997 $game_id = isset($_GET['game_id']) ? $_GET['game_id'] : 0;
998 $die = false;
999
1000 // Check game or map is really exists
1001 if($act == 'add' && !$this->acl_user_can('manage_game', 'all')) {
1002 $die = true;
1003 }
1004 else if($act == 'edit' || $act == 'maps' || $act == 'addmap') {
1005
1006 $g = $this->get_game(array('id' =>
1007 ($act == 'maps' || $act == 'addmap' ? $game_id : $id)
1008 ));
1009
1010 $die = empty($g) || !$this->acl_user_can('manage_game', $g[0]->id);
1011
1012 } else if($act == 'editmap') {
1013
1014 $m = $this->get_map(array('id' => $id));
1015 $die = empty($m);
1016 }
1017
1018 if($die)
1019 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
1020 $allowed_tags = array(
1021 'code' => array(),
1022 'em' => array()
1023 );
1024 if(sizeof($_POST)) {
1025
1026 $edit_maps_errors = array(
1027 self::ErrorDatabase => esc_html__('Database error.', 'arcane'),
1028 self::ErrorOK => esc_html__('The game is updated.', 'arcane'),
1029 self::ErrorUploadMaxFileSize => wp_kses(__('The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>.', 'arcane'), $allowed_tags ),
1030 self::ErrorUploadHTMLMaxFileSize => wp_kses(__('The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form.', 'arcane'), $allowed_tags ),
1031 self::ErrorUploadPartially => esc_html__('The uploaded file was only partially uploaded.', 'arcane'),
1032 self::ErrorUploadNoFile => esc_html__('No file was uploaded.', 'arcane'),
1033 self::ErrorUploadMissingTemp => esc_html__('Missing a temporary folder.', 'arcane'),
1034 self::ErrorUploadDiskWrite => esc_html__('Failed to write file to disk.', 'arcane'),
1035 self::ErrorUploadStoppedByExt => esc_html__('File upload stopped by extension.', 'arcane'),
1036 self::ErrorUploadFileTypeNotAllowed => esc_html__('File type does not meet security guidelines. Try another.', 'arcane')
1037 );
1038
1039 switch($act) {
1040 case 'add':
1041
1042 $defaults = array('title' => '', 'abbr' => '', 'icon' => 0, 'g_banner_file' => 0);
1043 $data = $this->extract_args(stripslashes_deep($_POST), $defaults);
1044 extract($data);
1045
1046 if(!empty($title)) {
1047
1048 $data['icon'] = $this->handle_upload('icon_file');
1049 $data['g_banner_file'] = $this->handle_upload('g_banner_file');
1050
1051 if($data['icon'] == self::ErrorUploadNoFile)
1052 $data['icon'] = 0;
1053
1054 if($data['g_banner_file'] == self::ErrorUploadNoFile)
1055 $data['g_banner_file'] = 0;
1056
1057 if($data['icon'] >= 0) {
1058
1059 if($this->add_game($data)) {
1060 wp_redirect(admin_url('admin.php?page=wp-teamwars-games&add=1'), $status = 302);
1061 exit();
1062 } else
1063 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1064 } else
1065 $this->add_notice($edit_maps_errors[$attach_id], 'error');
1066
1067 if($data['g_banner_file'] >= 0) {
1068
1069 if($this->add_game($data)) {
1070 wp_redirect(admin_url('admin.php?page=wp-teamwars-games&add=1'), $status = 302);
1071 exit();
1072 } else
1073 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1074 } else
1075 $this->add_notice($edit_maps_errors[$attach_id], 'error');
1076
1077
1078 } else
1079 $this->add_notice(esc_html__('Game title is required field.', 'arcane'), 'error');
1080 break;
1081
1082 case 'edit':
1083 $defaults = array('title' => '', 'abbr' => '', 'delete_image' => false, 'delete_image1' => false);
1084 $data = $this->extract_args(stripslashes_deep($_POST), $defaults);
1085 extract($data);
1086
1087 unset($data['delete_image']);
1088 unset($data['delete_image1']);
1089
1090 if(!empty($title)) {
1091
1092 if(!empty($delete_image))
1093 $data['icon'] = 0;
1094
1095 if(!empty($delete_image1))
1096 $data['g_banner_file'] = 0;
1097
1098 $attach_id = $this->handle_upload('icon_file');
1099 $attach_id1 = $this->handle_upload('g_banner_file');
1100
1101 if($attach_id == self::ErrorUploadNoFile)
1102 $attach_id = 0;
1103 else if($attach_id > 0)
1104 $data['icon'] = $attach_id;
1105
1106 if($attach_id1 == self::ErrorUploadNoFile)
1107 $attach_id1 = 0;
1108 else if($attach_id1 > 0)
1109 $data['g_banner_file'] = $attach_id1;
1110
1111
1112 if($attach_id >= 0) {
1113
1114 if($this->update_game($id, $data) !== false) {
1115 wp_redirect(admin_url('admin.php?page=wp-teamwars-games&update=1'), $status = 302);
1116 exit();
1117 } else
1118 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1119
1120 } else
1121 $this->add_notice($edit_maps_errors[$attach_id], 'error');
1122
1123
1124 if($attach_id1 >= 0) {
1125
1126 if($this->update_game($id, $data) !== false) {
1127 wp_redirect(admin_url('admin.php?page=wp-teamwars-games&update=1'), $status = 302);
1128 exit();
1129 } else
1130 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1131
1132 } else
1133 $this->add_notice($edit_maps_errors[$attach_id1], 'error');
1134
1135
1136
1137 } else
1138 $this->add_notice(esc_html__('Game title is required field.', 'arcane'), 'error');
1139 break;
1140
1141 case 'addmap':
1142 $defaults = array('title' => '', 'game_id' => 0, 'id' => 0);
1143 $data = $this->extract_args(stripslashes_deep($_POST), $defaults);
1144 extract($data);
1145
1146 if(!empty($title)) {
1147
1148 $attach_id = $this->handle_upload('screenshot_file');
1149
1150 if($attach_id == self::ErrorUploadNoFile)
1151 $attach_id = 0;
1152
1153 if($attach_id >= 0) {
1154
1155 if($this->add_map(array('title' => $title, 'screenshot' => $attach_id, 'game_id' => $game_id)) !== false) {
1156 wp_redirect(admin_url(sprintf('admin.php?page=wp-teamwars-games&act=maps&game_id=%d&add=1', $game_id)), $status = 302);
1157 exit();
1158 } else
1159 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1160
1161 } else
1162 $this->add_notice($edit_maps_errors[$attach_id], 'error');
1163
1164 } else
1165 $this->add_notice(esc_html__('Map title is required field.', 'arcane'), 'error');
1166
1167 break;
1168
1169 case 'editmap':
1170 $defaults = array('title' => '', 'game_id' => 'all', 'id' => 0, 'delete_image' => false);
1171 $data = $this->extract_args(stripslashes_deep($_POST), $defaults);
1172 extract($data);
1173
1174 $update_data = array('title' => $title);
1175
1176 if(!empty($title)) {
1177
1178 if(!empty($delete_image))
1179 $update_data['screenshot'] = 0;
1180
1181 $attach_id = $this->handle_upload('screenshot_file');
1182
1183 if($attach_id == self::ErrorUploadNoFile)
1184 $attach_id = 0;
1185 else if($attach_id > 0)
1186 $update_data['screenshot'] = $attach_id;
1187
1188 if($attach_id >= 0) {
1189
1190 if($this->update_map($id, $update_data) !== false) {
1191 wp_redirect(admin_url(sprintf('admin.php?page=wp-teamwars-games&act=maps&game_id=%d&update=1', $game_id)), $status = 302);
1192 exit();
1193 } else
1194 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
1195
1196 } else
1197 $this->add_notice($edit_maps_errors[$attach_id], 'error');
1198
1199 } else
1200 $this->add_notice(esc_html__('Map title is required field.', 'arcane'), 'error');
1201
1202 break;
1203 }
1204
1205 }
1206 }
1207
1208 function on_manage_games()
1209 {
1210 $act = isset($_GET['act']) ? $_GET['act'] : '';
1211 $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1;
1212 $filter_games = $this->acl_user_can('which_games');
1213 $limit = 10;
1214 $arcane_allowed = wp_kses_allowed_html( 'post' );
1215
1216 switch($act) {
1217 case 'add':
1218 return $this->on_add_game();
1219 break;
1220 case 'edit':
1221 return $this->on_edit_game();
1222 break;
1223 case 'maps':
1224 return $this->on_edit_maps();
1225 break;
1226 case 'addmap':
1227 return $this->on_add_map();
1228 break;
1229 case 'editmap':
1230 return $this->on_edit_map();
1231 break;
1232 }
1233
1234 $teams = $this->get_game(array(
1235 'id' => $filter_games,
1236 'orderby' => 'title', 'order' => 'asc',
1237 'limit' => $limit, 'offset' => ($limit * ($current_page-1))
1238 ));
1239 $stat = $this->get_game(array('id' => $filter_games, 'limit' => $limit), true);
1240
1241 $page_links = paginate_links( array(
1242 'base' => add_query_arg('paged', '%#%'),
1243 'format' => '',
1244 'prev_text' => esc_html__('«', 'arcane'),
1245 'next_text' => esc_html__('»', 'arcane'),
1246 'total' => $stat['total_pages'],
1247 'current' => $current_page
1248 ));
1249
1250 $page_links_text = sprintf( '<span class="displaying-num">' . esc_html__( 'Displaying %s–%s of %s', 'arcane' ) . '</span>%s',
1251 number_format_i18n( (($current_page - 1) * $limit) + 1 ),
1252 number_format_i18n( min( $current_page * $limit, $stat['total_items'] ) ),
1253 '<span class="total-type-count">' . number_format_i18n( $stat['total_items'] ) . '</span>',
1254 $page_links
1255 );
1256
1257 $table_columns = array('cb' => '<input type="checkbox" />',
1258 'title' => esc_html__('Title', 'arcane'),
1259 'abbr' => esc_html__('Abbreviation', 'arcane'),
1260 'id' => esc_html__('Game ID', 'arcane')
1261
1262 );
1263
1264 if(isset($_GET['add'])) {
1265 $this->add_notice(esc_html__('Game is successfully added.', 'arcane'), 'updated');
1266 }
1267
1268 if(isset($_GET['update'])) {
1269 $this->add_notice(esc_html__('Game is successfully updated.', 'arcane'), 'updated');
1270 }
1271
1272 if(isset($_GET['delete'])) {
1273 $deleted = (int)$_GET['delete'];
1274 $this->add_notice(sprintf(_n('%d Game deleted.', '%d Games deleted', $deleted, 'arcane'), $deleted), 'updated');
1275 }
1276
1277 $this->print_notices();
1278
1279 ?>
1280 <div class="wrap wp-cw-games">
1281 <h2><?php esc_html_e('Games', 'arcane'); ?>
1282 <?php if($this->acl_user_can('manage_game', 'all')) : ?> <a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=add')); ?>" class="add-new-h2"><?php esc_html_e('Add New', 'arcane'); ?></a><?php endif; ?>
1283 </h2>
1284
1285 <div id="poststuff" class="metabox-holder">
1286
1287 <div id="post-body">
1288 <div id="post-body-content" class="has-sidebar-content">
1289
1290 <form id="wp-teamwars-manageform" action="admin-post.php" method="post">
1291 <?php wp_nonce_field('wp-teamwars-gamesop'); ?>
1292
1293 <input type="hidden" name="action" value="wp-teamwars-gamesop" />
1294
1295 <div class="tablenav">
1296
1297 <div class="alignleft actions">
1298 <select name="do_action">
1299 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
1300 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
1301 <option value="export"><?php esc_html_e('Export', 'arcane'); ?></option>
1302 </select>
1303 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction" id="wp-teamwars-doaction" class="button-secondary action" />
1304 </div>
1305
1306 <div class="alignright actions">
1307 <label class="screen-reader-text" for="games-search-input"><?php esc_html_e('Search Teams:', 'arcane'); ?></label>
1308 <input id="games-search-input" name="s" value="<?php echo esc_html($search_title); ?>" type="text" />
1309
1310 <input id="games-search-submit" value="<?php esc_html_e('Search Games', 'arcane'); ?>" class="button" type="button" />
1311 </div>
1312
1313 <br class="clear" />
1314
1315 </div>
1316
1317 <div class="clear"></div>
1318
1319 <table class="widefat fixed" cellspacing="0">
1320 <thead>
1321 <tr>
1322 <?php $this->print_table_header($table_columns); ?>
1323 </tr>
1324 </thead>
1325
1326 <tfoot>
1327 <tr>
1328 <?php $this->print_table_header($table_columns, false); ?>
1329 </tr>
1330 </tfoot>
1331
1332 <tbody>
1333 <?php
1334 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
1335 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1336 } else {
1337 if (isset($_SERVER["REQUEST_URI"]))
1338 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1339 else
1340 $requesturi = null;
1341 }
1342
1343
1344 ?>
1345 <?php foreach($teams as $i => $item) : ?>
1346
1347 <tr class="iedit<?php if($i % 2 == 0) echo ' alternate'; ?>">
1348 <th scope="row" class="check-column"><input type="checkbox" name="items[]" value="<?php echo esc_attr($item->id); ?>" /></th>
1349 <td class="title column-title">
1350 <a class="row-title" href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=edit&id=' . $item->id)); ?>" title="<?php echo sprintf(esc_html__('Edit “%s” Team', 'arcane'), esc_attr($item->title)); ?>"> <?php echo esc_html($item->title); ?></a><br />
1351 <div class="row-actions">
1352 <span class="edit"><a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=edit&id=' . $item->id)); ?>"><?php esc_html_e('Edit', 'arcane'); ?></a></span> |
1353 <span class="edit"><a href="<?php echo admin_url('admin.php?page=wp-teamwars-games&act=maps&game_id=' . $item->id); ?>"><?php esc_html_e('Maps', 'arcane'); ?></a></span> | <span class="delete">
1354 <a href="<?php echo wp_nonce_url('admin-post.php?action=wp-teamwars-gamesop&do_action=delete&items[]=' . $item->id . '&_wp_http_referer=' . urlencode($requesturi), 'wp-teamwars-gamesop'); ?>"><?php esc_html_e('Delete', 'arcane'); ?></a></span>
1355 </div>
1356 </td>
1357 <td class="abbr column-abbr">
1358 <?php echo esc_html($item->abbr); ?>
1359 </td>
1360
1361 <td class="id column-id">
1362 <?php echo esc_html($item->id); ?>
1363 </td>
1364 </tr>
1365
1366 <?php endforeach; ?>
1367
1368 </tbody>
1369
1370 </table>
1371
1372 <div class="tablenav">
1373
1374 <div class="tablenav-pages"><?php echo wp_kses($page_links_text, $arcane_allowed); ?></div>
1375
1376 <div class="alignleft actions">
1377 <select name="do_action2">
1378 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
1379 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
1380 <option value="export"><?php esc_html_e('Export', 'arcane'); ?></option>
1381 </select>
1382 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction2" id="wp-teamwars-doaction2" class="button-secondary action" />
1383 </div>
1384
1385 <br class="clear" />
1386
1387 </div>
1388
1389 </form>
1390
1391 </div>
1392 </div>
1393 <br class="clear"/>
1394
1395 </div>
1396 </div>
1397 <?php
1398 }
1399
1400 function game_editor($page_title, $page_action, $page_submit, $game_id = 0)
1401 {
1402 $defaults = array('title' => '', 'icon' => 0, 'g_banner_file' => 0, 'abbr' => '', 'action' => '');
1403 $arcane_allowed = wp_kses_allowed_html( 'post' );
1404
1405 if($game_id > 0) {
1406 $t = $this->get_game(array('id' => $game_id));
1407 if(!empty($t))
1408 $data = (array)$t[0];
1409 }
1410
1411 extract($this->extract_args(stripslashes_deep($_POST), $this->extract_args($data, $defaults)));
1412
1413 $this->print_notices();
1414
1415 $attach = wp_get_attachment_image($icon, 'thumbnail');
1416 $attach1 = wp_get_attachment_image($g_banner_file, 'thumbnail');
1417
1418 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
1419 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1420 } else {
1421 if (isset($_SERVER["REQUEST_URI"]))
1422 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1423 else
1424 $requesturi = null;
1425 }
1426 ?>
1427
1428 <div class="wrap wp-cw-gameeditor">
1429 <h2><?php echo esc_attr($page_title); ?></h2>
1430
1431 <form name="team-editor" id="team-editor" method="post" action="<?php echo esc_attr($requesturi); ?>" enctype="multipart/form-data">
1432
1433 <input type="hidden" name="action" value="<?php echo esc_attr($page_action); ?>" />
1434 <input type="hidden" name="id" value="<?php echo esc_attr($game_id); ?>" />
1435
1436 <?php wp_nonce_field($page_action); ?>
1437
1438 <table class="form-table">
1439
1440 <tr class="form-field form-required">
1441 <th scope="row" valign="top"><label for="title"><span class="alignleft"><?php esc_html_e('Title', 'arcane'); ?></span><span class="alignright"><abbr title="<?php esc_html_e('required', 'arcane'); ?>" class="required">*</abbr></span><br class="clear" /></label></th>
1442 <td>
1443 <input name="title" id="title" type="text" class="regular-text" value="<?php echo esc_attr($title); ?>" maxlength="200" autocomplete="off" aria-required="true" />
1444 </td>
1445 </tr>
1446
1447 <tr class="form-field">
1448 <th scope="row" valign="top"><label for="title"><?php esc_html_e('Abbreviation', 'arcane'); ?></label></th>
1449 <td>
1450 <input name="abbr" id="abbr" type="text" class="regular-text" value="<?php echo esc_attr($abbr); ?>" maxlength="20" autocomplete="off" />
1451 </td>
1452 </tr>
1453
1454 <tr>
1455 <th scope="row" valign="top"><label for="icon_file"><?php esc_html_e('Icon', 'arcane'); ?></label></th>
1456 <td>
1457 <input type="file" name="icon_file" id="icon_file" />
1458
1459 <?php if(!empty($attach)) : ?>
1460 <div class="screenshot"><?php echo wp_kses($attach, $arcane_allowed); ?></div>
1461 <div>
1462 <label for="delete-image"><input type="checkbox" name="delete_image" id="delete-image" /> <?php esc_html_e('Delete Icon', 'arcane'); ?></label>
1463 </div>
1464 <?php endif; ?>
1465 </td>
1466 </tr>
1467
1468
1469 <tr>
1470 <th scope="row" valign="top"><label for="icon_file"><?php esc_html_e('Banner', 'arcane'); ?></label></th>
1471 <td>
1472 <input type="file" name="g_banner_file" id="g_banner_file" />
1473
1474 <?php if(!empty($attach1)) : ?>
1475 <div class="screenshot"><?php echo wp_kses($attach1, $arcane_allowed); ?></div>
1476 <div>
1477 <label for="delete-image1"><input type="checkbox" name="delete_image1" id="delete-image1" /> <?php esc_html_e('Delete Banner', 'arcane'); ?></label>
1478 </div>
1479 <?php endif; ?>
1480 </td>
1481 </tr>
1482
1483
1484 </table>
1485
1486 <p class="submit"><input type="submit" class="button-primary" name="submit" value="<?php echo esc_attr($page_submit); ?>" /></p>
1487
1488 </form>
1489
1490 </div>
1491
1492 <?php
1493 }
1494
1495 function _get_file_content($filename) {
1496 $content = '';
1497 WP_Filesystem();
1498 global $wp_filesystem;
1499
1500 $content = $wp_filesystem->get_contents($filename);
1501
1502 if($content) {
1503 return $content;
1504 }
1505
1506 return null;
1507 }
1508
1509 /*
1510 * Maps managment
1511 */
1512
1513 function on_admin_post_deletemaps()
1514 {
1515 if(!$this->acl_user_can('manage_games'))
1516 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
1517
1518 check_admin_referer('wp-teamwars-deletemaps');
1519
1520 $referer = remove_query_arg(array('add', 'update'), $_REQUEST['_wp_http_referer']);
1521
1522 if($_REQUEST['do_action'] == 'delete' || $_REQUEST['do_action2'] == 'delete') {
1523 extract($this->extract_args($_REQUEST, array('delete' => array())));
1524
1525 $error = $this->delete_map($delete);
1526 $referer = add_query_arg('delete', $error, $referer);
1527 }
1528
1529 wp_redirect($referer, $status = 302);
1530 }
1531
1532 function on_edit_maps()
1533 {
1534 $game_id = isset($_GET['game_id']) ? (int)$_GET['game_id'] : 0;
1535 $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1;
1536 $limit = 10;
1537 $arcane_allowed = wp_kses_allowed_html( 'post' );
1538
1539 $maps = $this->get_map('id=all&orderby=title&order=asc&game_id=' . $game_id . '&limit=' . $limit . '&offset=' . ($limit * ($current_page-1)));
1540 $stat = $this->get_map('id=all&game_id=' . $game_id . '&limit=' . $limit, true);
1541
1542 $page_links = paginate_links( array(
1543 'base' => add_query_arg('paged', '%#%'),
1544 'format' => '',
1545 'prev_text' => esc_html__('«', 'arcane'),
1546 'next_text' => esc_html__('»', 'arcane'),
1547 'total' => $stat['total_pages'],
1548 'current' => $current_page
1549 ));
1550
1551 $page_links_text = sprintf( '<span class="displaying-num">' . esc_html__( 'Displaying %s–%s of %s', 'arcane' ) . '</span>%s',
1552 number_format_i18n( (($current_page - 1) * $limit) + 1 ),
1553 number_format_i18n( min( $current_page * $limit, $stat['total_items'] ) ),
1554 '<span class="total-type-count">' . number_format_i18n( $stat['total_items'] ) . '</span>',
1555 $page_links
1556 );
1557
1558 $table_columns = array('cb' => '<input type="checkbox" />',
1559 'icon' => '',
1560 'title' => esc_html__('Title', 'arcane'));
1561
1562 if(isset($_GET['add'])) {
1563 $this->add_notice(esc_html__('Map is successfully added.', 'arcane'), 'updated');
1564 }
1565
1566 if(isset($_GET['update'])) {
1567 $this->add_notice(esc_html__('Map is successfully updated.', 'arcane'), 'updated');
1568 }
1569
1570 if(isset($_GET['delete'])) {
1571 $deleted = (int)$_GET['delete'];
1572 $this->add_notice(sprintf(_n('%d Map deleted.', '%d Maps deleted', $deleted, 'arcane'), $deleted), 'updated');
1573 }
1574
1575 $this->print_notices();
1576
1577 ?>
1578 <div class="wrap wp-cw-maps">
1579 <h2><?php esc_html_e('Maps', 'arcane'); ?> <a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=addmap&game_id=' . $game_id)); ?>" class="add-new-h2"><?php esc_html_e('Add New', 'arcane'); ?></a></h2>
1580
1581 <div id="poststuff" class="metabox-holder">
1582
1583 <div id="post-body">
1584 <div id="post-body-content" class="has-sidebar-content">
1585
1586 <form id="wp-teamwars-manageform" action="admin-post.php" method="post">
1587 <?php wp_nonce_field('wp-teamwars-deletemaps'); ?>
1588
1589 <input type="hidden" name="action" value="wp-teamwars-deletemaps" />
1590 <input type="hidden" name="game_id" value="<?php echo esc_attr($game_id); ?>" />
1591
1592 <div class="tablenav">
1593
1594 <div class="alignleft actions">
1595 <select name="do_action">
1596 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
1597 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
1598 </select>
1599 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction" id="wp-teamwars-doaction" class="button-secondary action" />
1600 </div>
1601
1602 <div class="alignright actions">
1603 <label class="screen-reader-text" for="maps-search-input"><?php esc_html_e('Search Maps:', 'arcane'); ?></label>
1604 <input id="maps-search-input" name="s" value="<?php echo esc_html($search_title); ?>" type="text" />
1605
1606 <input id="maps-search-submit" value="<?php esc_html_e('Search Maps', 'arcane'); ?>" class="button" type="button" />
1607 </div>
1608
1609 <br class="clear" />
1610
1611 </div>
1612
1613 <div class="clear"></div>
1614
1615 <table class="widefat fixed" cellspacing="0">
1616 <thead>
1617 <tr>
1618 <?php $this->print_table_header($table_columns); ?>
1619 </tr>
1620 </thead>
1621
1622 <tfoot>
1623 <tr>
1624 <?php $this->print_table_header($table_columns, false); ?>
1625 </tr>
1626 </tfoot>
1627
1628 <tbody>
1629 <?php
1630 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
1631 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1632 } else {
1633 if (isset($_SERVER["REQUEST_URI"]))
1634 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1635 else
1636 $requesturi = null;
1637 }
1638
1639
1640 ?>
1641 <?php foreach($maps as $i => $item) : ?>
1642
1643 <tr class="iedit<?php if($i % 2 == 0) echo ' alternate'; ?>">
1644 <th scope="row" class="check-column"><input type="checkbox" name="delete[]" value="<?php echo esc_attr($item->id); ?>" /></th>
1645 <td class="column-icon media-icon">
1646 <?php $attach = wp_get_attachment_image($item->screenshot, 'thumbnail');
1647 if(!empty($attach)) echo wp_kses($attach,$arcane_allowed);
1648 ?>
1649 </td>
1650 <td class="title column-title">
1651 <a class="row-title" href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=editmap&id=' . $item->id)); ?>" title="<?php echo sprintf(esc_html__('Edit “%s” Map', 'arcane'), esc_attr($item->title)); ?>"> <?php echo esc_html($item->title); ?></a><br />
1652 <div class="row-actions">
1653 <span class="edit"><a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-games&act=editmap&id=' . $item->id)); ?>"><?php esc_html_e('Edit', 'arcane'); ?></a></span> | <span class="delete">
1654 <a href="<?php echo wp_nonce_url('admin-post.php?action=wp-teamwars-deletemaps&do_action=delete&delete[]=' . $item->id . '&_wp_http_referer=' . urlencode($requesturi), 'wp-teamwars-deletemaps'); ?>"><?php esc_html_e('Delete', 'arcane'); ?></a></span>
1655 </div>
1656 </td>
1657 </tr>
1658
1659 <?php endforeach; ?>
1660
1661 </tbody>
1662
1663 </table>
1664
1665 <div class="tablenav">
1666
1667 <div class="tablenav-pages"><?php echo wp_kses($page_links_text,$arcane_allowed); ?></div>
1668
1669 <div class="alignleft actions">
1670 <select name="do_action2">
1671 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
1672 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
1673 </select>
1674 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction2" id="wp-teamwars-doaction2" class="button-secondary action" />
1675 </div>
1676
1677 <br class="clear" />
1678
1679 </div>
1680
1681 </form>
1682
1683 </div>
1684 </div>
1685 <br class="clear"/>
1686
1687 </div>
1688
1689 </div>
1690<?php
1691
1692 }
1693
1694 function get_map($p, $count = false)
1695 {
1696 global $wpdb;
1697
1698 extract($this->extract_args($p, array(
1699 'id' => false,
1700 'game_id' => false,
1701 'limit' => 0,
1702 'offset' => 0,
1703 'orderby' => 'id',
1704 'order' => 'ASC')));
1705
1706 $limit_query = '';
1707 $order_query = '';
1708
1709 $order = strtolower($order);
1710 if($order != 'asc' && $order != 'desc')
1711 $order = 'asc';
1712
1713 $order_query = 'ORDER BY `' . $orderby . '` ' . $order;
1714
1715 if($id != 'all' && $id !== false) {
1716
1717 if(!is_array($id))
1718 $id = array($id);
1719
1720 $id = array_map('intval', $id);
1721 $where_query = array();
1722 $where_query[] = 'id IN (' . implode(', ', $id) . ')';
1723 }
1724
1725 if($game_id != 'all' && $game_id !== false) {
1726
1727 if(!is_array($game_id))
1728 $game_id = array($game_id);
1729 if(!isset($where_query))$where_query = array();
1730 $game_id = array_map('intval', $game_id);
1731 $where_query[] = 'game_id IN (' . implode(', ', $game_id) . ')';
1732 }
1733
1734 if($limit > 0) {
1735 $limit_query = $wpdb->prepare('LIMIT %d, %d', $offset, $limit);
1736 }
1737
1738 if(!empty($where_query))
1739 $where_query = 'WHERE ' . implode(' AND ', $where_query);
1740
1741 if($count) {
1742
1743 $rslt = $wpdb->get_row('SELECT COUNT(id) AS m_count FROM `' . $this->tables['maps'] . '` ' . $where_query);
1744
1745 $ret = array('total_items' => 0, 'total_pages' => 1);
1746
1747 $ret['total_items'] = $rslt->m_count;
1748
1749 if($limit > 0)
1750 $ret['total_pages'] = ceil($ret['total_items'] / $limit);
1751
1752 return $ret;
1753 }
1754
1755 if(!isset($where_query))$where_query = '';
1756
1757 $rslt = $wpdb->get_results('SELECT * FROM `' . $this->tables['maps'] . '` ' . implode(' ', array($where_query, $order_query, $limit_query)));
1758
1759 return $rslt;
1760 }
1761
1762 function add_map($p)
1763 {
1764 global $wpdb;
1765
1766 $data = $this->extract_args($p, array(
1767 'title' => '',
1768 'screenshot' => 0,
1769 'game_id' => 0));
1770
1771 if($wpdb->insert($this->tables['maps'], $data, array('%s', '%d', '%d')))
1772 {
1773 $insert_id = $wpdb->insert_id;
1774
1775 return $insert_id;
1776 }
1777
1778 return false;
1779 }
1780
1781 function update_map($id, $p)
1782 {
1783 global $wpdb;
1784
1785 $fields = array('title' => '%s', 'screenshot' => '%d', 'game_id' => '%d');
1786
1787 $data = wp_parse_args($p, array());
1788
1789 $update_data = array();
1790 $update_mask = array();
1791
1792 foreach($fields as $fld => $mask) {
1793 if(isset($data[$fld])) {
1794 $update_data[$fld] = $data[$fld];
1795 $update_mask[] = $mask;
1796 }
1797 }
1798
1799 $result = $wpdb->update($this->tables['maps'], $update_data, array('id' => $id), $update_mask, array('%d'));
1800
1801 return $result;
1802 }
1803
1804 function delete_map($id)
1805 {
1806 global $wpdb;
1807
1808 if(!is_array($id))
1809 $id = array($id);
1810
1811 $id = array_map('intval', $id);
1812
1813 return $wpdb->query($wpdb->prepare('DELETE FROM `' . $this->tables['maps'] . '` WHERE id IN(%s)',implode(',', $id) ));
1814 }
1815
1816 function delete_map_by_game($id)
1817 {
1818 global $wpdb;
1819
1820 if(!is_array($id))
1821 $id = array($id);
1822
1823 $id = array_map('intval', $id);
1824
1825 return $wpdb->query($wpdb->prepare('DELETE FROM `' . $this->tables['maps'] . '` WHERE game_id IN(%s)',implode(',', $id) ));
1826 }
1827
1828 function on_add_map()
1829 {
1830 $game_id = isset($_GET['game_id']) ? (int)$_GET['game_id'] : 0;
1831
1832 $this->map_editor(esc_html__('Add Map', 'arcane'), 'wp-teamwars-addmap', esc_html__('Add Map', 'arcane'), $game_id);
1833 }
1834
1835 function on_edit_map()
1836 {
1837 $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
1838
1839 $this->map_editor(esc_html__('Edit Map', 'arcane'), 'wp-teamwars-editmap', esc_html__('Update Map', 'arcane'), 0, $id);
1840 }
1841
1842 function page_not_found($title, $message) {
1843
1844 echo '<div class="wrap"><h2>' . esc_attr($title) . '</h2>' . esc_attr($message) . '</div>';
1845
1846 }
1847
1848 function map_editor($page_title, $page_action, $page_submit, $game_id, $id = 0)
1849 {
1850 $defaults = array('title' => '', 'screenshot' => 0, 'abbr' => '', 'action' => '');
1851 $arcane_allowed = wp_kses_allowed_html( 'post' );
1852 if($id > 0) {
1853 $t = $this->get_map(array('id' => $id, 'game_id' => $game_id));
1854
1855 if(!empty($t)){
1856 $data = (array)$t[0];
1857 $game_id = $data['game_id'];
1858 }
1859 }
1860
1861 extract($this->extract_args(stripslashes_deep($_POST), $this->extract_args($data, $defaults)));
1862
1863 $attach = wp_get_attachment_image($screenshot, 'thumbnail');
1864
1865 $this->print_notices();
1866
1867 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
1868 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1869 } else {
1870 if (isset($_SERVER["REQUEST_URI"]))
1871 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
1872 else
1873 $requesturi = null;
1874 }
1875 ?>
1876
1877 <div class="wrap wp-cw-mapeditor">
1878 <h2><?php echo esc_attr($page_title); ?></h2>
1879
1880 <form name="map-editor" id="map-editor" method="post" action="<?php echo esc_attr($requesturi); ?>" enctype="multipart/form-data">
1881
1882 <input type="hidden" name="action" value="<?php echo esc_attr($page_action); ?>" />
1883 <input type="hidden" name="game_id" value="<?php echo esc_attr($game_id); ?>" />
1884 <input type="hidden" name="id" value="<?php echo esc_attr($id); ?>" />
1885
1886 <?php wp_nonce_field($page_action); ?>
1887
1888 <table class="form-table">
1889
1890 <tr class="form-field form-required">
1891 <th scope="row" valign="top"><label for="title"><span class="alignleft"><?php esc_html_e('Title', 'arcane'); ?></span><span class="alignright"><abbr title="<?php esc_html_e('required', 'arcane'); ?>" class="required">*</abbr></span><br class="clear" /></label></th>
1892 <td>
1893 <input name="title" id="title" type="text" class="regular-text" value="<?php echo esc_attr($title); ?>" maxlength="200" autocomplete="off" aria-required="true" />
1894 </td>
1895 </tr>
1896
1897 <tr>
1898 <th scope="row" valign="top"><label for="screenshot_file"><?php esc_html_e('Screenshot', 'arcane'); ?></label></th>
1899 <td>
1900 <input type="file" name="screenshot_file" id="screenshot_file" />
1901
1902 <?php if(!empty($attach)) : ?>
1903 <div class="screenshot"><?php echo wp_kses($attach,$arcane_allowed); ?></div>
1904 <div>
1905 <label for="delete-image"><input type="checkbox" name="delete_image" id="delete-image" /> <?php esc_html_e('Delete Screenshot', 'arcane'); ?></label>
1906 </div>
1907 <?php endif; ?>
1908 </td>
1909 </tr>
1910
1911 </table>
1912
1913 <p class="submit"><input type="submit" class="button-primary" name="submit" value="<?php echo esc_attr($page_submit); ?>" /></p>
1914
1915 </form>
1916
1917 </div>
1918
1919 <?php
1920 }
1921
1922 /*
1923 * Matches managment
1924 */
1925
1926 function on_admin_post_deletematches()
1927 {
1928 //UPDATED TO POSTS
1929 if(!$this->acl_user_can('manage_matches'))
1930 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
1931
1932 check_admin_referer('wp-teamwars-deletematches');
1933
1934 $referer = remove_query_arg(array('add', 'update'), $_REQUEST['_wp_http_referer']);
1935
1936 if($_REQUEST['do_action'] == 'delete' || $_REQUEST['do_action2'] == 'delete') {
1937 extract($this->extract_args($_REQUEST, array('delete' => array())));
1938
1939 $error = $this->delete_match($delete);
1940 $referer = add_query_arg('delete', $error, $referer);
1941 }
1942
1943 wp_redirect($referer, $status = 302);
1944 }
1945
1946 function current_time_fixed( $type, $gmt = 0 ) {
1947 $t = ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
1948 switch ( $type ) {
1949 case 'mysql':
1950 return $t;
1951 break;
1952 case 'timestamp':
1953 return strtotime($t);
1954 break;
1955 }
1956 }
1957
1958 function html_date_helper( $prefix, $time = 0, $tab_index = 0 )
1959 {
1960 global $wp_locale;
1961
1962 $tab_index_attribute = '';
1963 $tab_index = (int)$tab_index;
1964 if ($tab_index > 0)
1965 $tab_index_attribute = " tabindex=\"$tab_index\"";
1966
1967 if($time == 0)
1968 $time_adj = $this->current_time_fixed('timestamp', 0);
1969 else
1970 $time_adj = $time;
1971
1972 $jj = date( 'd', $time_adj );
1973 $mm = date( 'm', $time_adj );
1974 $hh = date( 'H', $time_adj );
1975 $mn = date( 'i', $time_adj );
1976 $yy = date( 'Y', $time_adj );
1977
1978 $month = "<select name=\"{$prefix}[mm]\"$tab_index_attribute>\n";
1979 for ( $i = 1; $i < 13; $i = $i +1 ) {
1980 $month .= "\t\t\t" . '<option value="' . zeroise($i, 2) . '"';
1981 if ( $i == $mm )
1982 $month .= ' selected="selected"';
1983 $month .= '>' . $wp_locale->get_month( $i ) . "</option>\n";
1984 }
1985 $month .= '</select>';
1986
1987 $day = '<input type="text" name="'.$prefix.'[jj]" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
1988 $hour = '<input type="text" name="'.$prefix.'[hh]" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
1989 $minute = '<input type="text" name="'.$prefix.'[mn]" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
1990 $year = '<input type="text" name="'.$prefix.'[yy]" value="' . $yy . '" size="3" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
1991
1992 printf(before_last_bar(esc_html__('%1$s%5$s %2$s @ %3$s : %4$s|1: month input, 2: day input, 3: hour input, 4: minute input, 5: year input', 'arcane')), $month, $day, $hour, $minute, $year);
1993 }
1994
1995 function date_array2time_helper($date)
1996 {
1997 if(is_array($date) &&
1998 isset($date['hh'], $date['mn'], $date['mm'], $date['jj'], $date['yy']))
1999 {
2000 return mktime($date['hh'], $date['mn'], 0, $date['mm'], $date['jj'], $date['yy']);
2001 }
2002
2003 return $date;
2004 }
2005
2006
2007
2008 function get_match($p, $count = false, $bez_turnira = false)
2009 {
2010
2011 extract($this->extract_args($p, array(
2012 'to_date' => 0,
2013 'from_date' => 0,
2014 'id' => false,
2015 'game_id' => false,
2016 'sum_tickets' => false,
2017 'limit' => 0,
2018 'offset' => 0,
2019 'orderby' => 'ID',
2020 'order' => 'ASC',
2021 'locked' => 0,
2022 'search' => false,
2023 'status' => false )));
2024
2025 if(is_numeric($id) AND ($id > 0)) {
2026 $obj_merged = (object) array_merge((array) get_post($id), (array) arcane_get_meta($id));
2027
2028 if (((isset($obj_merged->team1_tickets)) AND (isset($obj_merged->team2_tickets)) AND (($obj_merged->team1_tickets == 0) OR ($obj_merged->team2_tickets == 0))) OR (!isset($obj_merged->team1_tickets) AND !isset($obj_merged->team2_tickets))) {
2029
2030 $tickets = $this->SumTickets($obj_merged->ID);
2031
2032 if ((isset($obj_merged->team1_tickets) && $obj_merged->team1_tickets != $tickets[0]) OR (isset($obj_merged->team2_tickets) && $obj_merged->team2_tickets != $tickets[1])){
2033 update_post_meta($obj_merged->ID, 'team1_tickets', $tickets[0]);
2034 update_post_meta($obj_merged->ID, 'team2_tickets', $tickets[1]);
2035 $obj_merged->team1_tickets = $tickets[0];
2036 $obj_merged->team2_tickets = $tickets[1];
2037 }
2038
2039 }
2040
2041 return $obj_merged;
2042 }
2043
2044 $order = strtoupper($order);
2045 if($order != 'ASC' && $order != 'DESC')
2046 $order = 'ASC';
2047
2048 if ($count) {
2049 if ($search) {
2050 $args = array(
2051 'post_type' => 'matches',
2052 'posts_per_page' => -1,
2053 's' => $search,
2054 'post_status' => 'publish',
2055 );
2056 } else {
2057 $args = array(
2058 'post_type' => 'matches',
2059 'posts_per_page' => -1,
2060 'post_status' => 'publish',
2061 );
2062
2063 }
2064 } else {
2065 if ($search) {
2066 $args = array(
2067 'post_type' => 'matches',
2068 'posts_per_page' => -1,
2069 'offset' => $offset,
2070 'post_status' => 'publish',
2071 'orderby' => $orderby,
2072 's' => $search,
2073 'order' => $order,
2074 );
2075 } else {
2076 $args = array(
2077 'post_type' => 'matches',
2078 'posts_per_page' => $limit,
2079 'offset' => $offset,
2080 'post_status' => 'publish',
2081 'orderby' => $orderby,
2082 'order' => $order,
2083 );
2084 }
2085 }
2086
2087
2088
2089 $temp_metas = array();
2090
2091
2092 $counter = 0;
2093
2094 if($bez_turnira){
2095 $temp_metas[] = array(
2096 'key' => 'tournament_id',
2097 'compare' => 'NOT EXISTS'
2098 );
2099 }
2100
2101 if($status !== false ) {
2102
2103 if(!is_array($status))
2104 $status = array($status);
2105
2106 if (count ($status) > 1) {
2107 $temp = array();
2108 foreach ($status as $single) {
2109 $temp[] = array(
2110 'key' => 'status',
2111 'value' => $single,
2112 'compare' => '='
2113 );
2114 }
2115 $temp['relation'] = 'OR';
2116 $temp_metas[] = $temp;
2117 $counter++;
2118 }elseif(count ($status) == 1){
2119 $temp = array();
2120 foreach ($status as $single) {
2121 $temp[] = array(
2122 'key' => 'status',
2123 'value' => $single,
2124 'compare' => '='
2125 );
2126 }
2127 $temp_metas[] = $temp;
2128 $counter++;
2129
2130 }
2131 }
2132
2133 if($to_date > 0) {
2134 $temp_metas[] = array(
2135 'key' => 'date_unix',
2136 'value' => intval($to_date),
2137 'compare' => '<'
2138 );
2139
2140 }
2141
2142 if($from_date > 0) {
2143 $temp_metas[] = array(
2144 'key' => 'date_unix',
2145 'value' => intval($from_date),
2146 'compare' => '>='
2147 );
2148
2149 }
2150 if($game_id != 'all' && $game_id !== false) {
2151
2152 if(!is_array($game_id))
2153 $game_id = array($game_id);
2154
2155 if (count ($game_id) > 1) {
2156 $temp = array();
2157 foreach ($game_id as $single) {
2158 $temp[] = array(
2159 'key' => 'game_id',
2160 'value' => $single,
2161 'compare' => '='
2162 );
2163 }
2164 $temp['relation'] = 'OR';
2165 $temp_metas[] = $temp;
2166 $counter++;
2167 }elseif(count ($game_id) == 1){
2168 $temp = array();
2169 foreach ($game_id as $single) {
2170 $temp[] = array(
2171 'key' => 'game_id',
2172 'value' => $single,
2173 'compare' => '='
2174 );
2175 }
2176 $temp_metas[] = $temp;
2177 $counter++;
2178 }
2179 }
2180
2181
2182 if ($counter == 1) {
2183 $args['meta_query'] = $temp_metas;
2184 } elseif ($counter > 1) {
2185 $args['meta_query'] = $temp_metas;
2186 $args['meta_query']['relation'] = 'AND';
2187 }
2188
2189 $posts = get_posts($args);
2190
2191
2192 if($count) {
2193 if (is_array($posts)) {
2194 $ret['total_items'] = count($posts);
2195 if($limit > 0)
2196 $ret['total_pages'] = ceil($ret['total_items'] / $limit);
2197 return $ret;
2198 } else {
2199 $ret['total_items'] = 0;
2200 $ret['total_pages'] = 0;
2201 return $ret;
2202 }
2203
2204 }
2205
2206
2207 $returner = array();
2208 $counter = 0;
2209 foreach( $posts as $single) {
2210 $returner[$counter] = (object) array_merge((array) $single, (array) arcane_get_meta($single->ID));
2211 $counter ++;
2212 }
2213
2214 return $returner;
2215 }
2216
2217 function SumTickets($mid) {
2218 //UPDATED FOR POSTS
2219 if ($mid > 0) {
2220 $current = get_post_meta($mid, 'tickets', true);
2221 $data[0] = 0;
2222 $data[1] = 0;
2223 if (is_array($current) AND (count($current) > 0)) {
2224 foreach ($current as $single) {
2225 if (isset($single['tickets1'])) {
2226 $data[0] = $data[0] + intval($single['tickets1']);
2227 }
2228 if (isset($single['tickets2'])) {
2229 $data[1] = $data[1] + intval($single['tickets2']);
2230 }
2231 }
2232 }
2233 return $data;
2234 }
2235 }
2236
2237
2238
2239 function update_match_post($match_id, $tid = 0, $type="team") {
2240 //UPDATED FOR POSTS
2241 $id = $match_id;
2242 $post_category = get_option(ARCANE_TEAMWARS_CATEGORY, -1);
2243 $postarr = array(
2244 'post_status' => 'publish',
2245 'post_content' => '',
2246 'post_excerpt' => '',
2247 'post_title' => '',
2248 'post_type' => 'matches',
2249 'comment_status' => 'open'
2250
2251 );
2252
2253 if($post_category != -1)
2254 $postarr['post_category'] = array((int)$post_category);
2255 $post = $this->get_match(array('id' => $match_id, 'sum_tickets' => true));
2256
2257 $m = $post;
2258
2259 if(!is_null($post)) {
2260 $postarr['ID'] = $post->ID;
2261 }
2262
2263 if(isset($post->tournament_id) && !empty($post->tournament_id)){
2264 $tourn_id = $post->tournament_id;
2265 $games = get_post_meta($tourn_id, 'game_cache', true);
2266
2267 if(is_array($games)){
2268 foreach ($games as &$game) {
2269
2270 foreach ($game as &$single_game) {
2271 if($single_game['match_post_id'] == $post->ID && !empty($post->date_unix)){
2272 if($post->date_unix != $single_game['time']){
2273 $single_game['time'] = $post->date_unix;
2274 }
2275 }
2276 }
2277 }
2278 }
2279
2280 update_post_meta($tourn_id, 'game_cache', $games);
2281 }
2282
2283
2284 if(isset($post->tournament_participants))
2285 $cache_particip = $post->tournament_participants;
2286 if ($post->tournament_participants == "team") {
2287 $t1a = $this->get_team(array('id' => $post->team1));
2288 $t1 = $t1a->post_title;
2289 $t2a = $this->get_team(array('id' => $post->team2));
2290 $t2 = $t2a->post_title;
2291 } else {
2292
2293 $t1a = get_user_by('id', $post->team1);
2294 $t1 = $t1a->display_name;
2295 $t2a = get_user_by('id', $post->team2);
2296 $t2 = $t2a->display_name;
2297 }
2298
2299 if(empty($post->post_title)) {
2300
2301
2302 if(!empty($t1) && !empty($t2))
2303 $post_title = sprintf(esc_html__('%1$s Versus %2$s', 'arcane'), $t1, $t2);
2304 else
2305 $post_title = esc_html__('Regular match', 'arcane');
2306 }
2307
2308 $post_excerpt = '';
2309 $scores = $this->SumTickets($match_id);
2310 $team1_title = $t1;
2311 $team2_title = $t2;
2312
2313 $t1 = $scores[0];
2314 $t2 = $scores[1];
2315 $round_class1 = $t1 < $t2 ? 'lose' : ($t1 > $t2 ? 'victory' : 'draw');
2316 $round_class2 = $t1 > $t2 ? 'lose' : ($t1 < $t2 ? 'victory' : 'draw');
2317 $wl_class1 = $t1 < $t2 ? 'lose' : ($t1 > $t2 ? 'win' : '');
2318 $wl_class2 = $t1 > $t2 ? 'lose' : ($t1 < $t2 ? 'win' : '');
2319 if (!isset($post_content)) {
2320 $post_content = '';
2321 }
2322 $post_content .= '<div id="matches" class="tab-pane match-page">
2323 <div class="profile-fimage match-fimage ">
2324 <div class="mminfow"><div class="mminfo"><strong>'.$m->title.'</strong> <i class="fas fa-calendar-alt"></i> ' . date(get_option('date_format').' '.get_option('time_format'), strtotime($m->date)) . '</div></div>
2325 <div class="dots"></div>
2326 <img alt="img" class="attachment-small wp-post-image" src="'.arcane_return_game_banner($m->game_id).'">
2327 <div class="matched" id="mtch">';
2328 if(empty($match_status))$match_status='';
2329 if(($match_status == 'active' || $match_status == 'rejected' || $match_status == 'submitted1' || $match_status == 'submitted2' || $match_status == 'pending') && $admin){
2330 $post_content .= '<a mid="'.$match_id.'" href="javascript:void(0);" class="ajaxdeletematch"><i data-original-title="Delete Match" data-toggle="tooltip" class="fas fa-times"></i></a>';
2331 }
2332 if(isset($admin) && $admin){
2333 $post_content .= '<a href="javascript:void(0);"><i data-original-title="Edit Match" data-toggle="tooltip" class="fas fa-cog"></i></a>';
2334 }
2335
2336 $post_content .= '</div><div class="team-a">
2337 <div class="teamimgw">
2338 <img alt="img" src="'.arcane_return_team_image_big($m->team1, $type).'" />
2339 <div class="teammfs '.$wl_class1.'"><span>'.$t1.'</span></div>
2340 </div>
2341 <div class="pmi_title">'. $team1_title . '</div>
2342 </div>
2343 <div class="mversus">'.esc_html__('vs', 'arcane').'</div>
2344 <div class="team-b">
2345 <div class="teamimgw">
2346 <img alt="img" src="'.arcane_return_team_image_big($m->team2, $type).'" />
2347 <div class="teammfs '.$wl_class2.'"><span>'.$t2.'</span></div>
2348 </div>
2349 <div class="pmi_title">'. $team2_title . ' </div>
2350 </div>
2351 <div class="clear"></div>
2352 <div class="col-lg-12 col-md-12 nav-top-divider"></div>
2353 </div>
2354 <div class="col-lg-12 col-md-12 block ">
2355 <div id="score_fin" class="mcscalert">
2356 Score has been submitted by <a>teamnamea</a>! - <span>Accept the score?</span> <a class="ajaxsubmitscore" href="javascript:void(0);" req="accept_score" mid="'.$match_id.'"><i class="fas fa-check"></i></a> <a class="ajaxsubmitscore" href="javascript:void(0);" req="reject_score" mid="'.$match_id.'"><i class="fas fa-times"></i></a>
2357 </div>
2358 </div>
2359 <div class="col-lg-6 col-md-6 block mdescription">
2360 <div class="title-wrapper">
2361 <h3 class="widget-title"><i class="fas fa-bullhorn"></i>'.esc_html__(" Match description", "arcane").'</h3></div>
2362 <div class="wcontainer">'.wpautop(do_shortcode(stripslashes(wp_kses_post($m->description)))).'
2363 </div>
2364 </div>';
2365
2366 $post_content .=
2367 '<div class="col-lg-6 col-md-6 block mmaps block">
2368 <div class="title-wrapper"><h3 class="widget-title"><i class="far fa-image"></i>'.esc_html__(" Maps", "arcane").'</h3></div>
2369 <ul>';
2370
2371
2372 $r = $this->get_rounds($match_id);
2373
2374 $rounds = array();
2375
2376 // group rounds by map
2377 foreach($r as $v) {
2378
2379 if(!isset($rounds[$v->group_n]))
2380 $rounds[$v->group_n] = array();
2381
2382 $rounds[$v->group_n][] = $v;
2383 }
2384
2385
2386 // render maps/rounds
2387 foreach($rounds as $map_group) {
2388
2389 $first = $map_group[0];
2390 $image = wp_get_attachment_image_src($first->screenshot);
2391
2392
2393
2394 $post_content .= '<li>';
2395
2396 if(!empty($image))
2397 $post_content .= '<img src="' . $image[0] . '" alt="' . esc_attr($first->title) . '" />';
2398 $post_content .= '<strong>' . esc_html($first->title) . '</strong>';
2399 $post_content .= '<div class="mscorew">';
2400 foreach($map_group as $round) {
2401
2402 $t1 = $round->tickets1;
2403 $t2 = $round->tickets2;
2404
2405 if($m->status == 'active'){
2406
2407 $round_class = 'notsubmitted';
2408 }else{
2409 $round_class = $t1 < $t2 ? 'mlose' : ($t1 > $t2 ? 'mwin' : 'mtie');
2410 }
2411
2412
2413 $post_content .= '<div class="mscore">';
2414 $post_content .= sprintf(esc_html__('%1$d:%2$d', 'arcane'), $t1, $t2);
2415 $post_content .= '</div>';
2416
2417 }
2418 $post_content .= '</div><div class="clear"></div></li>';
2419 }
2420
2421
2422
2423
2424
2425 $post_content .='</ul></div>';
2426
2427 $post_excerpt .= sprintf(_x('%1$s vs %2$s', 'match_excerpt', 'arcane'), $m->team1_title, $m->team2_title) . "\n";
2428
2429 $postarr['post_title'] = $m->title;
2430 $postarr['post_content'] = $post_content;
2431 $postarr['post_excerpt'] = wp_trim_excerpt($post_excerpt);
2432 $postarr['ID'] = $match_id;
2433
2434
2435 wp_update_post($postarr);
2436 update_post_meta($match_id, 'team1_tickets', $scores[0]);
2437 update_post_meta($match_id, 'team2_tickets', $scores[1]);
2438 $games = $this->get_game(array('id' => get_post_meta($match_id, 'game_id', true)));
2439 if (isset($games[0])) {
2440 update_post_meta($match_id, 'game_title', $games[0]->title);
2441 update_post_meta($match_id, 'game_icon', $games[0]->icon);
2442 }
2443 if ($tid > 0) {
2444 update_post_meta($match_id, 'match_status', 1);
2445 update_post_meta($match_id, 'tournament_id', $tid);
2446 }
2447
2448 update_post_meta($match_id, 'tournament_participants', $cache_particip);
2449
2450 return $match_id;
2451
2452
2453
2454 }
2455
2456 function add_match($p)
2457 {
2458 //CONVERTED TO POSTS
2459
2460 $cpage= get_current_screen();
2461 if (is_object($cpage)) {
2462 if($cpage->base == 'teamwars_page_wp-teamwars-matches'){
2463 $data = $this->extract_args($p, array(
2464 'title' => '',
2465 'date' => $this->current_time_fixed('timestamp', 0),
2466 'date_unix' => '',
2467 'post_id' => 0,
2468 'team1' => 0,
2469 'team2' => 0,
2470 'game_id' => 0,
2471 'match_status' => 0,
2472 'description' => '',
2473 'external_url' => '',
2474 'status' => 'active',
2475 'locked' => 0
2476 ));
2477 }else{
2478 $data = $this->extract_args($p, array(
2479 'title' => '',
2480 'date' => $this->current_time_fixed('timestamp', 0),
2481 'date_unix' => '',
2482 'post_id' => 0,
2483 'team1' => 0,
2484 'team2' => 0,
2485 'game_id' => 0,
2486 'match_status' => 0,
2487 'description' => '',
2488 'external_url' => '',
2489 'status' => 'pending',
2490 'locked' => 0
2491 ));
2492 }
2493 } else {
2494 $data = $this->extract_args($p, array(
2495 'title' => '',
2496 'date' => $this->current_time_fixed('timestamp', 0),
2497 'date_unix' => '',
2498 'post_id' => 0,
2499 'team1' => 0,
2500 'team2' => 0,
2501 'game_id' => 0,
2502 'match_status' => 0,
2503 'description' => '',
2504 'external_url' => '',
2505 'status' => 'pending',
2506 'locked' => 0
2507 ));
2508
2509 }
2510
2511 $post_category = get_option(ARCANE_TEAMWARS_CATEGORY, -1);
2512 $postarr = array(
2513 'post_status' => 'publish',
2514 'post_content' => '',
2515 'post_excerpt' => '',
2516 'post_title' => $data['title'],
2517 'post_name' => sanitize_title($data['title']),
2518 'post_type' => 'matches',
2519 'comment_status' => 'open'
2520 );
2521 if($post_category != -1){
2522 $postarr['post_category'] = array((int)$post_category);
2523 }
2524
2525 $new_post_ID = wp_insert_post($postarr);
2526 if ($new_post_ID) {
2527 arcane_mass_update_post_meta($new_post_ID, $data);
2528 return $new_post_ID;
2529 } else {
2530 return false;
2531 }
2532
2533 return $new_post_ID;
2534 }
2535
2536 function update_match($id, $p)
2537 {
2538 global $ArcaneWpTeamWars;
2539 //CONVERTED TO POSTS
2540 $data = wp_parse_args($p, array());
2541 if(isset($data['external_url'])) {
2542 $data['external_url'] = esc_url_raw($data['external_url']);
2543 }
2544
2545 arcane_mass_update_post_meta($id, $data);
2546
2547 if (isset($data['status'])) {
2548 $tid = get_post_meta($id, 'tournament_id', true);
2549
2550 $tournament = new arcane_tournaments();
2551
2552 if ($tournament->LoadTournament($tid)) {
2553 $m = $this->get_match(array('id' => $id, 'sum_tickets' => true));
2554
2555 if ( (isset($m->status)) AND (!empty($m->status)) AND (strtolower($m->status) == "done")) {
2556 $newdata['tid'] = $tid;
2557 $newdata['match_postid'] = $id;
2558 $newdata['match_id'] = $id;
2559
2560 if ($tid > 0){
2561 $obj_merged = (object) array_merge((array) get_post($m->ID), (array) arcane_get_meta($m->ID));
2562 $tickets = $ArcaneWpTeamWars->SumTickets($obj_merged->ID);
2563 $t1 = $tickets[0];
2564 $t2 = $tickets[1];
2565 } else {
2566
2567 $t1 = $m->team1_tickets;
2568 $t2 = $m->team2_tickets;
2569
2570 }
2571 $newdata['team1_score'] = $t1;
2572 $newdata['team2_score'] = $t2;
2573 if ($t1 > $t2) {
2574 $newdata['winner'] = 'team1';
2575 } elseif ($t1 == $t2) {
2576 $newdata['winner'] = 'draw';
2577 } else {
2578 //$t2 > $t1
2579 $newdata['winner'] = 'team2';
2580 }
2581 if($data['status'] == 'done')
2582 $tournament->tournament->OnResultSubmit($newdata);
2583
2584 } elseif ( (isset($m->status)) AND (!empty($m->status)) AND (strtolower($m->status) == "active")) {
2585 $newdata['tid'] = $tid;
2586 $newdata['match_postid'] = $id;
2587 $newdata['match_id'] = $id;
2588 if($data['status'] == 'done')
2589 $tournament->tournament->OnResultSubmit($newdata);
2590 }
2591 }
2592 }
2593
2594 return true;
2595 }
2596
2597 function delete_match($id)
2598 {
2599 //CONVERTED TO POSTS
2600 if(!is_array($id))
2601 $id = array($id);
2602
2603 $id = array_map('intval', $id);
2604
2605 $this->delete_rounds_by_match($id);
2606 if (is_array($id)) {
2607 foreach ($id as $small_id) {
2608 wp_delete_post($small_id);
2609 }
2610 } else {
2611 wp_delete_post($id);
2612 }
2613 return true;
2614 }
2615
2616 function delete_match_by_team($id) {
2617 //CONVERTED TO POSTS
2618
2619 if(!is_array($id))
2620 $id = array($id);
2621
2622 $id = array_map('intval', $id);
2623 if (is_array($id)) {
2624 foreach ($id as $small_id) {
2625 $args = array(
2626 'post_type' => 'matches',
2627 'posts_per_page' => -1,
2628 'post_status' => 'any',
2629 'meta_query' => array(
2630 'relation' => 'OR',
2631 array(
2632 'key' => 'team1',
2633 'value' => $small_id,
2634 'compare' => '=',
2635 'type' => 'numeric',
2636 ),
2637 array(
2638 'key' => 'team2',
2639 'value' => $small_id,
2640 'compare' => '=',
2641 'type' => 'numeric',
2642 ),
2643 )
2644 );
2645
2646 $posts = get_posts($args);
2647
2648 if (is_array($posts) AND (count($posts) > 0) ) {
2649 foreach ($posts as $post) {
2650 wp_delete_post($post->ID);
2651 }
2652 }
2653 }
2654 }
2655 return true;
2656 }
2657
2658 function delete_match_by_game($id) {
2659 //UPDATED FOR POSTS
2660 $args = array(
2661 'post_type' => 'matches',
2662 'meta_key' => 'game_id',
2663 'meta_value' => $id
2664 );
2665 $posts = get_posts($args);
2666 if (is_array($posts)) {
2667 foreach ($posts as $post) {
2668 wp_delete_post($post->ID);
2669 }
2670 }
2671 }
2672
2673 function get_rounds($match_id)
2674 {
2675 //UPDATED FOR POSTS
2676
2677 $rounds = get_post_meta($match_id, 'tickets', true);
2678 if (is_array($rounds)) {
2679 $temparray = array();
2680 foreach ($rounds as $round) {
2681 $m = $this->get_map(array('id' => $round['map_id']));
2682 $round['title'] = $m[0]->title;
2683 $round['screenshot'] = $m[0]->screenshot;
2684
2685 $temparray[] = (object)$round;
2686 }
2687 return $temparray;
2688 } else {
2689 return array();
2690 }
2691
2692 }
2693
2694 function add_round($p)
2695 {
2696 //UPDATED TO POSTS
2697 $data = $this->extract_args($p, array(
2698 'match_id' => 0,
2699 'group_n' => 0,
2700 'map_id' => 0,
2701 'tickets1' => 0,
2702 'tickets2' => 0
2703 ));
2704 if(is_admin() && ($data['tickets1'] != 0 or $data['tickets2'] != 0)){
2705 update_post_meta($data['match_id'], 'status', 'done');
2706 }
2707 $tickets = get_post_meta($data['match_id'], 'tickets', true);
2708
2709 if (is_array($tickets)) {
2710 $counter = count($tickets);
2711 //find new empty ticket ID
2712 while (isset($tickets[$counter])) {
2713 $counter++;
2714 }
2715 $newdata = array_map('intval', $data);
2716 $tickets[$counter] = $newdata;
2717 if(update_post_meta($data['match_id'], 'tickets', $tickets)) {
2718 return $counter;
2719 } else {
2720 return false;
2721 }
2722 } else {
2723 $tickets = array();
2724 $tickets[1] = array_map('intval', $data);
2725
2726 if(update_post_meta($data['match_id'], 'tickets', $tickets)) {
2727 return 1;
2728 } else {
2729 return false;
2730 }
2731 }
2732 }
2733
2734 function update_round($id, $p)
2735 {
2736 //UPDATED TO POSTS
2737 $data = wp_parse_args($p, array());
2738
2739
2740
2741 if(is_admin() && ($data['tickets1'] != 0 or $data['tickets2'] != 0)){
2742 update_post_meta($data['match_id'], 'status', 'done');
2743 }
2744
2745 $tickets = get_post_meta($data['match_id'], 'tickets', true);
2746
2747 if (is_array($tickets)) {
2748 $newdata = array_map('intval', $data);
2749
2750 $tickets[$id] = $newdata;
2751
2752 if(update_post_meta($data['match_id'], 'tickets', $tickets)) {
2753 return $counter;
2754 } else {
2755 return false;
2756 }
2757 } else {
2758 return false;
2759 }
2760
2761 }
2762
2763 function delete_rounds_not_in($match_id, $id)
2764 {
2765 //UPDATED FOR POSTS
2766 if(!is_array($id))
2767 $id = array($id);
2768
2769 $newrounds = array();
2770 $rounds = get_post_meta($match_id, 'tickets', true);
2771 if (is_array($rounds)) {
2772 foreach ($rounds as $key => $value) {
2773 if (in_array($key, $id)) {
2774 $newrounds[$key] = $value;
2775 }
2776 }
2777 }
2778 $reset = array_values($newrounds);
2779 update_post_meta($match_id, 'tickets', $reset);
2780 return true;
2781 }
2782
2783 function delete_rounds_by_match($match_id)
2784 {
2785 //UPDATED FOR POSTS
2786 update_post_meta($match_id, 'tickets', '');
2787 return true;
2788 }
2789
2790 function on_add_match()
2791 {
2792 return $this->match_editor(esc_html__('Add Match', 'arcane'), 'wp-teamwars-matches', esc_html__('Add Match', 'arcane'));
2793 }
2794
2795 function on_edit_match()
2796 {
2797 $id = isset($_GET['id']) ? $_GET['id'] : 0;
2798
2799 return $this->match_editor(esc_html__('Edit Match', 'arcane'), 'wp-teamwars-matches', esc_html__('Edit Match', 'arcane'), $id);
2800 }
2801
2802 function on_ajax_get_maps()
2803 {
2804 /* if(!$this->acl_user_can('manage_games') &&
2805 !$this->acl_user_can('manage_matches'))
2806 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
2807 */
2808
2809 $game_id = isset($_POST['game_id']) ? (int)$_POST['game_id'] : 0;
2810
2811 if($game_id > 0) {
2812 $maps = $this->get_map(array('game_id' => $game_id, 'order' => 'asc', 'orderby' => 'title'));
2813
2814 for($i = 0; $i < sizeof($maps); $i++) {
2815 $url = wp_get_attachment_thumb_url($maps[$i]->screenshot);
2816
2817 $maps[$i]->screenshot_url = !empty($url) ? $url : '';
2818 }
2819
2820 echo json_encode($maps); die();
2821 }
2822 }
2823
2824 function match_editor($page_title, $page_action, $page_submit, $id = 0)
2825 {
2826
2827 //UPDATED FOR POSTS
2828 $data = array();
2829 $current_time = $this->current_time_fixed('timestamp', 0);
2830 $post_id = 0;
2831
2832 $defaults = array('game_id' => 0,
2833 'title' => '',
2834 'team1' => 0,
2835 'team2' => 0,
2836 'scores' => array(),
2837 'match_status' => 0,
2838 'action' => '',
2839 'description' => '',
2840 'external_url' => '',
2841 'status' => '',
2842 'date' => array('mm' => date('m', $current_time),
2843 'yy' => date('Y', $current_time),
2844 'jj' => date('j', $current_time),
2845 'hh' => date('H', $current_time),
2846 'mn' => date('i', $current_time)),
2847 'reported_reason' => '',
2848 'locked' => 0);
2849
2850
2851 if($id > 0) {
2852 $t = $this->get_match(array('id' => $id));
2853 $keep_match = $t;
2854
2855 if(!empty($t)){
2856 $data = (array)$t;
2857
2858 $data['id'] = $data['ID'];
2859 $data['date'] = strtotime($data['date']);
2860 $data['scores'] = array();
2861
2862 $post_id = $data['id'];
2863 $rounds = $this->get_rounds($data['id']);
2864
2865 $data['participants'] = get_post_meta($data['id'], 'tournament_participants', true);
2866 $data['tid'] = get_post_meta($data['id'], 'tournament_id', true);
2867
2868 if (is_array($rounds)) {
2869
2870 foreach($rounds as $round) {
2871 $data['scores'][$round->group_n]['map_id'] = $round->map_id;
2872 $data['scores'][$round->group_n]['round_id'][] = $round->id;
2873 $data['scores'][$round->group_n]['team1'][] = $round->tickets1;
2874 $data['scores'][$round->group_n]['team2'][] = $round->tickets2;
2875 }
2876 }
2877 }
2878 }
2879
2880 $games = $this->get_game(array('id' => $this->acl_user_can('which_games'), 'orderby' => 'title', 'order' => 'asc'));
2881 $teams = $this->get_team('id=all&orderby=post_title&order=asc');
2882
2883 extract($this->extract_args(stripslashes_deep($_POST), $this->extract_args($data, $defaults)));
2884
2885
2886 $date = $this->date_array2time_helper($date);
2887
2888 if(isset($_GET['update'])) {
2889 $this->add_notice(esc_html__('Match is successfully updated.', 'arcane'), 'updated');
2890 }
2891
2892 $this->print_notices();
2893 if (!isset($data['participants'])) {
2894 $data['participants'] = get_post_meta($post_id, 'tournament_participants', true);
2895 }
2896
2897 ?>
2898
2899 <div class="wrap wp-cw-matcheditor">
2900
2901 <h2><?php echo esc_attr($page_title); ?>
2902 <?php if($post_id) : ?>
2903 <ul class="linkbar">
2904 <li class="post-link"><a href="<?php echo esc_attr(get_permalink($post_id)); ?>" target="_blank" class="icon-link"><?php echo esc_attr($post_id); ?></a></li>
2905 <li class="post-comments"><a href="<?php echo esc_url(get_comments_link($post_id)); ?>" target="_blank"><?php echo esc_attr(get_comments_number($post_id)); ?></a></li>
2906 </ul>
2907 <?php endif; ?>
2908
2909 <?php
2910 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
2911 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
2912 } else {
2913 if (isset($_SERVER["REQUEST_URI"]))
2914 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
2915 else
2916 $requesturi = null;
2917 }
2918
2919 ?>
2920 </h2>
2921
2922 <form name="match-editor" id="match-editor" method="post" action="<?php echo esc_attr($requesturi); ?>" enctype="multipart/form-data">
2923
2924 <input type="hidden" name="action" value="<?php echo esc_attr($page_action); ?>" />
2925 <input type="hidden" name="id" value="<?php echo esc_attr($id); ?>" />
2926
2927 <?php wp_nonce_field($page_action); ?>
2928
2929 <table class="form-table">
2930
2931 <tr class="form-field form-required">
2932 <th scope="row" valign="top"><label for="game_id"><?php esc_html_e('Game', 'arcane'); ?></label></th>
2933 <td>
2934 <?php if (isset($data['tid']) && ($data['tid'] > 0)) { echo '<div class="nestani">';} ?>
2935 <select id="game_id" name="game_id" >
2936 <?php foreach($games as $item) : ?>
2937 <option value="<?php echo esc_attr($item->id); ?>"<?php selected($item->id, $game_id); ?>><?php echo esc_html($item->title); ?></option>
2938 <?php endforeach; ?>
2939 </select>
2940 <?php if (isset($data['tid']) && ($data['tid'] > 0)) { echo '</div>';} ?>
2941 <?php if (!isset($data['tid']) OR !($data['tid'] > 0)) { ?><p class="description"><?php esc_html_e('The scores will be removed on game change.', 'arcane'); ?></p><?php }
2942 else { ?>
2943 <p class="description"><?php esc_html_e('Tournament games cannot have games changed.', 'arcane'); ?></p>
2944 <?php }
2945 ?>
2946 </td>
2947 </tr>
2948
2949 <tr class="form-field form-required">
2950 <th scope="row" valign="top"><label for="title"><?php esc_html_e('Title', 'arcane'); ?></label></th>
2951 <td>
2952 <input name="title" id="title" type="text" value="<?php echo esc_attr($title); ?>" maxlength="200" autocomplete="off" aria-required="true" />
2953 </td>
2954 </tr>
2955
2956 <tr class="form-field">
2957 <th scope="row" valign="top"><label for="description"><?php esc_html_e('Description', 'arcane'); ?></label></th>
2958 <td>
2959 <?php $settings = array( 'textarea_name' => 'description' );
2960 wp_editor( $description, 'description', $settings ); ?>
2961 </td>
2962 </tr>
2963 <?php
2964 if (!(isset($data['tid']) AND ($data['tid'] > 0))) {
2965 ?>
2966 <tr class="form-field">
2967 <th scope="row" valign="top"><label for="external_url"><?php esc_html_e('External URL', 'arcane'); ?></label></th>
2968 <td>
2969 <input type="text" name="external_url" id="external_url" value="<?php echo esc_attr($external_url); ?>" />
2970
2971 <p class="description"><?php esc_html_e('Enter league URL or external match URL.', 'arcane'); ?></p>
2972 </td>
2973 </tr>
2974
2975 <tr class="form-required">
2976 <th scope="row" valign="top"><label for=""><?php esc_html_e('Match type', 'arcane'); ?></label></th>
2977 <td>
2978 <?php foreach($this->match_status as $index => $text) : ?>
2979 <label for="match_status_<?php echo esc_attr($index); ?>"><input type="radio" value="<?php echo esc_attr($index); ?>" name="match_status" id="match_status_<?php echo esc_attr($index); ?>"<?php checked($index, $match_status, true); ?> /> <?php echo esc_attr($text); ?></label><br/>
2980 <?php endforeach; ?>
2981 </td>
2982 </tr>
2983 <?php } ?>
2984 <?php
2985 if (!(isset($data['tid']) && ($data['tid'] > 0))) {
2986
2987 ?>
2988 <tr class="form-required">
2989 <th scope="row" valign="top"><label for=""><?php esc_html_e('Participant type', 'arcane'); ?></label></th>
2990 <td>
2991 <label for="match_type_game"><input type="radio" <?php if ($data['participants'] == 'team') { echo 'checked="checked"'; } ?> name="match_game_type" class='matchgametype' value='team'/><?php esc_html_e('Team match', 'arcane'); ?></label><br/>
2992 <label for="match_type_game"><input type="radio" <?php if ($data['participants'] == 'user') { echo 'checked="checked"'; } ?> name="match_game_type" class='matchgametype' value='user'/><?php esc_html_e('Individual match', 'arcane'); ?></label><br/>
2993 </td>
2994 </tr>
2995 <?php
2996 } else {
2997 echo '<input type="hidden" name="match_game_type" class="matchgametype" value="'.esc_attr($data['participants']).'" /><br/>';
2998 }
2999 ?>
3000
3001 <tr class="form-required">
3002 <th scope="row" valign="top"><label for=""><?php esc_html_e('Date', 'arcane'); ?></label></th>
3003 <td>
3004 <?php $this->html_date_helper('date', $date); ?>
3005 </td>
3006 </tr>
3007
3008 <tr class="form-required">
3009 <th scope="row" valign="top"></th>
3010 <td>
3011 <div class="match-results" id="matchsite">
3012
3013 <div class="teams">
3014 <?php if (isset($data['tid']) AND ($data['tid'] > 0)) {
3015 //is a tournament game
3016 if ($data['participants'] != 'team') {
3017 $u1 = get_user_by('id', $team1);
3018 $u2 = get_user_by('id', $team2);
3019 echo esc_html__('Player', 'arcane')." 1: ".esc_attr($u1->display_name)."<br />";
3020 echo esc_html__('Player', 'arcane')." 2: ".esc_attr($u2->display_name)."<br />";
3021 $allusers = get_users();
3022 ?>
3023 <div id="userselector">
3024 <select name="team1-user" class="team-select-user">
3025 <?php foreach($allusers as $t) : ?>
3026 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected($t->ID, $team1); ?>><?php echo esc_attr($t->display_name); ?></option>
3027 <?php endforeach; ?>
3028 </select> <?php esc_html_e('vs', 'arcane'); ?>
3029 <select name="team2-user" class="team-select-user">
3030 <?php foreach($allusers as $t) : ?>
3031 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected($t->ID, $team2); ?>><?php echo esc_attr($t->display_name); ?></option>
3032 <?php endforeach; ?>
3033 </select>
3034 </div>
3035 <?php
3036 } else {
3037 $u1 = get_post($team1);
3038 $u2 =get_post($team2);
3039 echo esc_html__('Player', 'arcane')." 1: ".esc_attr($u1->post_title)."<br />";
3040 echo esc_html__('Player', 'arcane')." 2: ".esc_attr($u2->post_title)."<br />";
3041 ?>
3042 <div id="teamselector">
3043 <select name="team1-team" class="team-select-team">
3044 <?php foreach($teams as $t) : ?>
3045 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $team1 > 0 ? ($t->ID == $team1) : $t->home_team, true); ?>><?php echo esc_attr($t->title); ?></option>
3046 <?php endforeach; ?>
3047 </select> <?php esc_html_e('vs', 'arcane'); ?>
3048 <select name="team2-team" class="team-select-team">
3049 <?php foreach($teams as $t) : ?>
3050 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $t->ID==$team2, true); ?>><?php echo esc_attr($t->title); ?></option>
3051 <?php endforeach; ?>
3052 </select>
3053 </div>
3054 <?php
3055
3056 }
3057
3058 ?>
3059
3060 <?php
3061 } else {
3062
3063 //is not, first show team selector
3064 ?>
3065
3066 <div id="teamselector">
3067 <select name="team1-team" class="team-select-team">
3068
3069 <?php
3070
3071 foreach($teams as $t) :
3072 ?>
3073 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $team1 > 0 ? ($t->ID == $team1) : $t->home_team, true); ?>><?php echo esc_attr($t->title); ?></option>
3074 <?php endforeach; ?>
3075 </select> <?php esc_html_e('vs', 'arcane'); ?>
3076 <select name="team2-team" class="team-select-team">
3077 <?php foreach($teams as $t) : ?>
3078 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $t->ID==$team2, true); ?>><?php echo esc_attr($t->title); ?></option>
3079 <?php endforeach; ?>
3080
3081 </select>
3082 </div>
3083 <!-- <?php
3084 $allusers = get_users();
3085
3086 ?>
3087 <div id="userselector">
3088 <select name="team1-user" class="team-select-user">
3089
3090 <?php foreach($allusers as $t) : ?>
3091 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $team1 > 0 ? ($t->ID == $team1) : $t->home_team, true); ?>><?php echo esc_attr($t->display_name); ?></option>
3092 <?php endforeach; ?>
3093 </select> <?php esc_html_e('vs', 'arcane'); ?>
3094 <select name="team2-user" class="team-select-user">
3095 <?php foreach($allusers as $t) : ?>
3096 <option value="<?php echo esc_attr($t->ID); ?>"<?php selected(true, $t->ID==$team2, true); ?>><?php echo esc_attr($t->display_name); ?></option>
3097 <?php endforeach; ?>
3098
3099 </select>
3100 </div> -->
3101 </div>
3102
3103 <div class="team2-inline">
3104 <label for="new_team_title"><?php esc_html_e('or just type opponent team here:', 'arcane'); ?></label><br/>
3105 <input name="new_team_title" id="new_team_title" type="text" value="" maxlength="200" autocomplete="off" aria-required="true" />
3106
3107 </div>
3108 <br class="clear"/>
3109 <?php
3110 }
3111 ?>
3112
3113
3114 <div id="mapsite"></div>
3115
3116 <div class="add-map" id="wp-cw-addmap">
3117 <input type="button" class="button button-secondary" value="<?php esc_html_e('Add map', 'arcane'); ?>" />
3118 </div>
3119
3120 </div>
3121 </td>
3122 </tr>
3123
3124
3125
3126
3127 <tr class="form-field form-required">
3128 <th scope="row" valign="top"><label for="locked"><?php esc_html_e('Lock this match', 'arcane'); ?></label></th>
3129 <td>
3130 <input type="checkbox" name="locked" id="locked" value="1" <?php if($locked == 1){ ?> checked <?php } ?> >
3131 <p class="description"><?php esc_html_e('Lock it. Prevent users from deleting/editing/flagging and submitting score.', 'arcane'); ?></p>
3132 </td>
3133 </tr>
3134 <?php $status = $keep_match->status; ?>
3135
3136 <?php if($status == 'submitted1' or $status == 'submitted2'){ ?>
3137 <tr class="form-field form-required">
3138 <th scope="row" valign="top"><label for="locked"><?php esc_html_e('Score submitted by', 'arcane'); ?></label></th>
3139 <td>
3140 <p class="description"><?php if($status == 'submitted1'){
3141 $name = arcane_return_team_name_by_team_id($team1);
3142 echo esc_attr($name);
3143 }elseif($status == 'submitted2'){
3144 $name = arcane_return_team_name_by_team_id($team2);
3145 echo esc_attr($name);
3146 } ?></p>
3147 </td>
3148 </tr>
3149 <?php } ?>
3150
3151 <?php if($status == 'reported1' or $status == 'reported2'){ ?>
3152 <tr class="form-field">
3153 <th scope="row" valign="top"><label for=""><?php esc_html_e('Reported by', 'arcane'); ?></label></th>
3154 <td>
3155 <?php if($status == 'reported1'){
3156 $name = arcane_return_team_name_by_team_id($team1);
3157 echo esc_attr($name);
3158 }elseif($status == 'reported2'){
3159 $name = arcane_return_team_name_by_team_id($team2);
3160 echo esc_attr($name);
3161 } ?>
3162 </td>
3163 </tr>
3164 <tr class="form-field">
3165 <th scope="row" valign="top"><label for=""><?php esc_html_e('Reason for report', 'arcane'); ?></label></th>
3166 <td>
3167 <textarea name="reason" id="reason" value="<?php echo esc_attr($reported_reason); ?>" cols="25" rows="10" autocomplete="off" aria-required="true" ><?php echo esc_attr($reported_reason); ?></textarea>
3168 </td>
3169 </tr>
3170 <?php } ?>
3171 </table>
3172
3173 <p class="submit"><input type="submit" class="button-primary" id="wp-cw-submit" name="submit" value="<?php echo esc_attr($page_submit); ?>" />
3174
3175 <?php if($status == 'reported1' or $status == 'reported2'){ ?>
3176
3177 <input type="submit" class="button-primary" id="wp-cw-submit-resolve" name="submit-resolve" value="<?php esc_html_e('Resolve', 'arcane'); ?>" />
3178
3179 <?php } ?>
3180
3181 </p>
3182 </form>
3183
3184 </div>
3185
3186 <?php
3187 }
3188
3189 function quick_pick_team($title) {
3190 $team = $this->get_team(array('title' => $title, 'limit' => 1));
3191 $team_id = 0;
3192 if(empty($team)) {
3193 $new_team_id = $this->add_team(array('title' => $title));
3194 if($new_team_id !== false)
3195 $team_id = $new_team_id;
3196 } else {
3197 $team_id = $team[0]->id;
3198 }
3199
3200 return $team_id;
3201 }
3202
3203 function on_load_manage_matches()
3204 {
3205
3206 $id = isset($_REQUEST['id']) ? $_GET['id'] : 0;
3207 $act = isset($_GET['act']) ? $_GET['act'] : '';
3208
3209
3210 // Check match is really exists
3211 if($act == 'edit') {
3212 $m = $this->get_match(array('id' => $id));
3213 if($id != 0 && empty($m))
3214 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
3215
3216 if(!$this->acl_user_can('manage_game', $m->game_id))
3217 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
3218 }
3219
3220 if(sizeof($_POST) > 0)
3221 {
3222
3223
3224 if(isset($_POST['game_id']) && !$this->acl_user_can('manage_game', $_POST['game_id']))
3225 wp_die( esc_html__('Cheatin’ uh?', 'arcane') );
3226
3227 switch($act) {
3228
3229 case 'add':
3230
3231 extract($this->extract_args(stripslashes_deep($_POST), array(
3232 'game_id' => 0,
3233 'title' => '',
3234 'description' => '',
3235 'external_url' => '',
3236 'date' => $this->current_time_fixed('timestamp', 0),
3237 'team1' => 0,
3238 'team2' => 0,
3239 'scores' => array(),
3240 'new_team_title' => '',
3241 'match_status' => 0,
3242 'locked' => 0
3243 )));
3244
3245 $date = $this->date_array2time_helper($date);
3246
3247 if(!empty($new_team_title)) {
3248 $pickteam = $this->quick_pick_team($new_team_title);
3249
3250 if($pickteam > 0)
3251 $team2 = $pickteam;
3252 }
3253 if ($_POST['match_game_type'] == 'team' or $_POST['match_game_type'] === NULL) {
3254 $team1 = $_POST['team1-team'];
3255 $team2 = $_POST['team2-team'];
3256 } else {
3257 $team1 = $_POST['team1-user'];
3258 $team2 = $_POST['team2-user'];
3259 }
3260 $match_id = $this->add_match(array(
3261 'title' => $title,
3262 'date' => date('Y-m-d H:i:s', $date),
3263 'date_unix' => $date,
3264 'post_id' => 0,
3265 'team1' => $team1,
3266 'team2' => $team2,
3267 'game_id' => $game_id,
3268 'match_status' => $match_status,
3269 'description' => $description,
3270 'locked' => $locked
3271 ));
3272
3273 if($match_id) {
3274
3275 foreach($scores as $round_group => $r) {
3276 for($i = 0; $i < sizeof($r['team1']); $i++) {
3277 $this->add_round(array('match_id' => $match_id,
3278 'group_n' => abs($round_group),
3279 'map_id' => $r['map_id'],
3280 'tickets1' => $r['team1'][$i],
3281 'tickets2' => $r['team2'][$i]
3282 ));
3283 }
3284 }
3285
3286
3287 if ($_POST['match_game_type'] == 'team' or $_POST['match_game_type'] === NULL) {
3288
3289 $this->update_match_post($match_id, 0, 'team');
3290 } else {
3291 $this->update_match_post($match_id, 0, 'user');
3292 }
3293 wp_redirect(admin_url('admin.php?page=wp-teamwars-matches&add=1'), $status = 302);
3294 exit();
3295 } else
3296 $this->add_notice(esc_html__('An error occurred.', 'arcane'), 'error');
3297
3298 break;
3299
3300 case 'edit':
3301
3302 $pid = $_POST['id'];
3303
3304 $tuid = get_post_meta($pid, 'tournament_id', true);
3305
3306 $tournament_timezone = get_post_meta($tuid, 'tournament_timezone' , true);
3307 if(!$tournament_timezone) {
3308 $timezone_string = arcane_timezone_string();
3309 $tournament_timezone = $timezone_string ? $timezone_string : 'UTC';
3310 }
3311 date_default_timezone_set($tournament_timezone);
3312
3313 extract($this->extract_args(stripslashes_deep($_POST), array(
3314 'id' => 0,
3315 'game_id' => 0,
3316 'title' => '',
3317 'description' => '',
3318 'external_url' => '',
3319 'date' => $this->current_time_fixed('timestamp', 0),
3320 'team1' => 0,
3321 'team2' => 0,
3322 'new_team_title' => '',
3323 'match_status' => 0,
3324 'scores' => array(),
3325 'locked' => 0
3326 )));
3327
3328
3329 update_post_meta($pid, 'tournament_participants', $_POST['match_game_type']);
3330 if (($_POST['match_game_type'] == 'team') or !isset($_POST['match_game_type']) ) {
3331 $team1 = $_POST['team1-team'];
3332 $team2 = $_POST['team2-team'];
3333 } else {
3334 $team1 = $_POST['team1-user'];
3335 $team2 = $_POST['team2-user'];
3336 }
3337
3338 if(isset($_POST) && $_POST['submit-resolve'] == 'Resolve'){
3339 $this->update_match($id, array(
3340 'title' => $title,
3341 'date' => date('Y-m-d H:i:s', strtotime(mktime($date['hh'], $date['mn'], 0, $date['mm'], $date['jj'], $date['yy'])) ),
3342 'team1' => $team1,
3343 'team2' => $team2,
3344 'game_id' => $game_id,
3345 'match_status' => $match_status,
3346 'description' => $description,
3347 'external_url' => $external_url,
3348 'status' => 'active',
3349 'locked' => $locked
3350
3351 ));
3352
3353 }else{
3354 $datum = date('Y-m-d H:i:s', mktime($date['hh'], $date['mn'], 0, $date['mm'], $date['jj'], $date['yy']) );
3355
3356 $this->update_match($id, array(
3357 'title' => $title,
3358 'date' => date('Y-m-d H:i:s', mktime($date['hh'], $date['mn'], 0, $date['mm'], $date['jj'], $date['yy']) ),
3359 'team1' => $team1,
3360 'team2' => $team2,
3361 'game_id' => $game_id,
3362 'match_status' => $match_status,
3363 'description' => $description,
3364 'external_url' => $external_url,
3365 'locked' => $locked
3366
3367 ));
3368
3369 }
3370 $date = $this->date_array2time_helper($date);
3371
3372 if(!empty($new_team_title)) {
3373 $pickteam = $this->quick_pick_team($new_team_title);
3374
3375 if($pickteam > 0)
3376 $team2 = $pickteam;
3377 }
3378
3379
3380 $rounds_not_in = array();
3381
3382 foreach($scores as $round_group => $r) {
3383 for($i = 0; $i < sizeof($r['team1']); $i++) {
3384 $round_id = $r['round_id'][$i];
3385 $round_data = array('match_id' => $id,
3386 'group_n' => abs($round_group),
3387 'map_id' => $r['map_id'],
3388 'tickets1' => $r['team1'][$i],
3389 'tickets2' => $r['team2'][$i]
3390 );
3391
3392 if($round_id > 0) {
3393 $this->update_round($round_id, $round_data);
3394 $rounds_not_in[] = $round_id;
3395 } else {
3396 $new_round = $this->add_round($round_data);
3397 if($new_round !== false)
3398 $rounds_not_in[] = $new_round;
3399 }
3400 }
3401 }
3402
3403 $this->delete_rounds_not_in($id, $rounds_not_in);
3404
3405 $this->update_match_post($id);
3406
3407 $match = $this->get_match(array('id' => $id, 'sum_tickets' => true));
3408
3409
3410 $tid = get_post_meta($pid, 'tournament_id', true);
3411 $tournament = new arcane_tournaments();
3412 if ($tournament->LoadTournament($tid)) {
3413 if ( (isset($match->status)) AND (!empty($match->status)) AND (strtolower($match->status) == "done")) {
3414 $newdata['tid'] = $tid;
3415 $newdata['match_postid'] = $pid;
3416 $newdata['match_id'] = $id;
3417 $newdata['team1_score'] = $match->team1_tickets;
3418 $newdata['team2_score'] = $match->team2_tickets;
3419 if ($match->team1_tickets > $match->team2_tickets) {
3420 $newdata['winner'] = 'team1';
3421 } elseif ($match->team1_tickets == $match->team2_tickets) {
3422 $newdata['winner'] = 'draw';
3423 } else {
3424 //$t2 > $t1
3425 $newdata['winner'] = 'team2';
3426 }
3427 $tournament->tournament->OnResultSubmit($newdata);
3428 }
3429 }
3430
3431 wp_redirect(admin_url('admin.php?page=wp-teamwars-matches&act=edit&id=' . $id . '&update=1'), $status = 302);
3432 exit();
3433
3434 break;
3435 }
3436 }
3437 }
3438
3439 function on_shortcode($atts) {
3440
3441 $output = '';
3442
3443 extract(shortcode_atts(array('per_page' => 20), $atts));
3444
3445 $per_page = abs($per_page);
3446 $current_page = get_query_var('paged');
3447 $now = $this->current_time_fixed('timestamp');
3448 $current_game = isset($_GET['game']) ? $_GET['game'] : false;
3449
3450 $games = $this->get_game('id=all&orderby=title&order=asc');
3451
3452 if($current_page < 1)
3453 $current_page = 1;
3454
3455 $p = array(
3456 'limit' => $per_page,
3457 'sum_tickets' => true,
3458 'game_id' => $current_game,
3459 'offset' => ($current_page-1) * $per_page,
3460 'status' => array('active', 'done')
3461 );
3462
3463 $matches = $this->get_match($p, false, true);
3464
3465 $stat = $this->get_match($p, true, true);
3466
3467 $page_links = paginate_links( array(
3468 'base' => add_query_arg('paged', '%#%'),
3469 'format' => '',
3470 'prev_text' => esc_html__('«', 'arcane'),
3471 'next_text' => esc_html__('»', 'arcane'),
3472 'total' => $stat['total_pages'],
3473 'current' => $current_page
3474 ));
3475
3476 $page_links_text = sprintf( '<span class="displaying-num">' . esc_html__( 'Displaying %s–%s of %s', 'arcane' ) . '</span>%s',
3477 number_format_i18n( (($current_page - 1) * $per_page) + 1 ),
3478 number_format_i18n( min( $current_page * $per_page, $stat['total_items'] ) ),
3479 '<span class="total-type-count">' . number_format_i18n( $stat['total_items'] ) . '</span>',
3480 $page_links
3481 );
3482
3483
3484 $output .= '<div class="teamwarlist-page widget">';
3485 $output .= '<ul class="teamwar-list asd">';
3486 $output .= '<li><ul class="tabs">';
3487
3488 if(count($games) > 1){
3489 $obj = new stdClass();
3490 $obj->id = 0;
3491 $obj->title = esc_html__('All', 'arcane');
3492 $obj->abbr = esc_html__('All', 'arcane');
3493 $obj->icon = 0;
3494
3495 array_unshift($games, $obj);
3496 }
3497
3498 for($i = 0; $i < sizeof($games); $i++) :
3499 $game = $games[$i];
3500 $link = ($game->id == 0) ? 'all' : 'game-' . $game->id;
3501 if($i == 0){$class = 'selected';}else{$class = '';}
3502 $p = array( 'game_id' => $game->id,'status' => array('active', 'done'));
3503 $matches_tab = $this->get_match($p, false);
3504 if(!empty($matches_tab)){
3505 $output .= '<li class="'.$class.'"><a href="#'.$link.'" title="'.esc_attr($game->title).'">'.esc_html($game->abbr).'</a></li>';
3506 }
3507 endfor;
3508 $output .= '</ul> <div class="clear"></div>
3509 </li>';
3510 // generate table content
3511 $j=0;
3512 foreach($matches as $index => $match) {
3513 if($match->status == 'active' || $match->status == 'done'){
3514 if($i % 2 != 0) $alt = ' alt';
3515 $output .= '<li title="' . esc_attr($match->title) . '" class="teamwar-item '.$alt.' match ' . ' game-'.$match->game_id.' ">';
3516
3517 // output match status
3518 $is_upcoming = false;
3519 $t1 = $match->team1_tickets;
3520 $t2 = $match->team2_tickets;
3521
3522 if(isset($match->date_unix) && !empty($match->date_unix)){
3523 $date = date_i18n(get_option('date_format') . ', ' . get_option('time_format'), $match->date_unix);
3524 $timestamp = $match->date_unix;
3525 }else{
3526 $date = mysql2date(get_option('date_format') . ', ' . get_option('time_format'), $match->date);
3527 $timestamp = mysql2date('U', $match->date);
3528 }
3529
3530 global $wpdb;
3531 $table = array(
3532 'teams' => 'cw_teams',
3533 'games' => 'cw_games',
3534 );
3535 $table = array_map(create_function('$t', 'global $table_prefix; return $table_prefix . $t; '), $table);
3536 $team1id = $match->team1;
3537 $team2id = $match->team2;
3538
3539 if ($match->tournament_participants == "team" or $match->tournament_participants == NULL) {
3540 //teamtype
3541 $img_url1 = arcane_return_team_image($match->team1);
3542 $image1 = arcane_aq_resize( $img_url1, 50, 50, true, true, true ); //resize & crop img
3543 $match->team1_title = get_the_title($match->team1);
3544
3545
3546 $img_url2 = arcane_return_team_image($match->team2);
3547 $image2 = arcane_aq_resize( $img_url2, 50, 50, true, true, true ); //resize & crop img
3548 $match->team2_title = get_the_title($match->team2);
3549 } else {
3550 $img_url1 = get_user_meta($match->team1, 'profile_photo', true);
3551 if (!empty($img_url1)) {
3552 $image1 = arcane_aq_resize( $img_url1, 50, 50, true, true, true ); //resize & crop img
3553 }
3554 $match->team1_title = get_the_author_meta("display_name", $match->team1);
3555
3556 $img_url2 = get_user_meta($match->team2, 'profile_photo', true);
3557 if (!empty($img_url2)) {
3558 $image2 = arcane_aq_resize( $img_url2, 50, 50, true, true, true ); //resize & crop img
3559 }
3560 $match->team2_title = get_the_author_meta("display_name", $match->team2);
3561 }
3562
3563 $is_upcoming = $timestamp > $now;
3564 $is_playing = ( ($now > $timestamp && $now < $timestamp + 3600) && ($t1 == 0 && $t2 == 0) || ($match->status == 'active') && ($t1 == 0 && $t2 == 0) );
3565
3566 // output game icon
3567 $game_icon = wp_get_attachment_url($match->game_icon);
3568
3569 // teams
3570 $output .= '<div class="wrap"><a href="' . get_permalink($match->ID) . '" data-toggle="tooltip" data-original-title="' . esc_attr($match->title) . '">';
3571 if($is_upcoming) :
3572 $output .= '<div class="upcoming">' . esc_html__('Upcoming', 'arcane') . '</div>';
3573 elseif($is_playing) :
3574 $output .= '<div class="playing">' . esc_html__('Playing', 'arcane') . '</div>';
3575 else :
3576 $output .= '<div class="scores">' . sprintf(esc_html__('%1$d:%2$d', 'arcane'), $t1, $t2) . '</div>';
3577 endif;
3578
3579 $output .= '<div class="match-wrap">';
3580
3581 if(!isset($image1) or empty($image1)){ $image1 = get_template_directory_uri().'/img/defaults/default-team-50x50.jpg'; }
3582 if(!isset($image2) or empty($image2)){ $image2 = get_template_directory_uri().'/img/defaults/default-team-50x50.jpg'; }
3583
3584 $output .= '<img src="'.$image1.'" class="team1img" alt="'.esc_html($match->team2_title).'">';
3585 $output .= '<div class="vs">'.esc_html__("VS", "arcane").'</div><div class="opponent-team"><img src="'.$image2.'" class="team1img" alt="'.esc_html($match->team2_title).'"></div>';
3586 $team2_title = esc_html($match->team2_title);
3587 $output .= '<div class="clear"></div>';
3588
3589 $output .= '</div>';
3590
3591 $output .= '<div class="date"><strong>' . esc_attr($match->title) . '</strong>'. esc_html($date) . '</div>';
3592 $output .= '<div class="clear"></div>';
3593
3594
3595 $output .= '</a>';
3596 $output .= '</div>';
3597 $output .= '</li>';
3598 $j++;
3599 }}
3600
3601 $output .= '</ul>';
3602 $output .= '</div>';
3603 $output .= '<div class="wp-teamwars-pagination clearfix">' .$page_links_text . '</div>';
3604
3605 return $output;
3606
3607 }
3608
3609 function on_manage_matches()
3610 {
3611 //tusi
3612 $act = isset($_GET['act']) ? $_GET['act'] : '';
3613 $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1;
3614 $limit = 10;
3615 $game_filter = $this->acl_user_can('which_games');
3616 $arcane_allowed = wp_kses_allowed_html( 'post' );
3617 if(isset($_GET['s']))
3618 $search = $_GET['s'];
3619 if (isset($search)) {
3620 $search = urldecode($search);
3621 $search = sanitize_text_field($search);
3622 } else {
3623 $search = "";
3624 }
3625 switch($act) {
3626 case 'add':
3627 return $this->on_add_match();
3628 break;
3629 case 'edit':
3630 return $this->on_edit_match();
3631 break;
3632 }
3633
3634 $stat_condition = array(
3635 'id' => 'all',
3636 'game_id' => $game_filter,
3637 'limit' => $limit
3638 );
3639
3640 $condition = array(
3641 'id' => 'all', 'game_id' => $game_filter, 'sum_tickets' => true,
3642 'orderby' => 'id', 'order' => 'desc', 'search' =>$search,
3643 'limit' => $limit, 'offset' => ($limit * ($current_page-1))
3644
3645 );
3646
3647 $matches = $this->get_match($condition);
3648 $stat = $this->get_match($stat_condition, true);
3649 if (!empty($search)) {
3650
3651 $filtered = array();
3652 foreach ($matches as $match) {
3653
3654 if (strpos (strtolower($match->post_title), strtolower($search)) !== false) {
3655 $filtered[] = $match;
3656 }
3657 }
3658 $matches = $filtered;
3659 $stat['total_items'] = count($matches);
3660 if($limit > 0)
3661 $stat['total_pages'] = ceil($stat['total_items'] / $limit);
3662 }
3663
3664
3665 $page_links = paginate_links( array(
3666 'base' => add_query_arg('paged', '%#%'),
3667 'format' => '',
3668 'prev_text' => esc_html__('«', 'arcane'),
3669 'next_text' => esc_html__('»', 'arcane'),
3670 'total' => $stat['total_pages'],
3671 'current' => $current_page
3672 ));
3673
3674 $page_links_text = sprintf( '<span class="displaying-num">' . esc_html__( 'Displaying %s–%s of %s', 'arcane' ) . '</span>%s',
3675 number_format_i18n( (($current_page - 1) * $limit) + 1 ),
3676 number_format_i18n( min( $current_page * $limit, $stat['total_items'] ) ),
3677 '<span class="total-type-count">' . number_format_i18n( $stat['total_items'] ) . '</span>',
3678 $page_links
3679 );
3680
3681 $table_columns = array('cb' => '<input type="checkbox" />',
3682 'title' => esc_html__('Title', 'arcane'),
3683 'game_title' => esc_html__('Game', 'arcane'),
3684 'date' => esc_html__('Date', 'arcane'),
3685 'match_status' => esc_html__('Match status', 'arcane'),
3686 'team1' => esc_html__('Team 1', 'arcane'),
3687 'team2' => esc_html__('Team 2', 'arcane'),
3688 'tickets' => esc_html__('Tickets', 'arcane'),
3689 'reported' => esc_html__('Reported', 'arcane'),
3690 'locked' => esc_html__('Locked', 'arcane'),
3691 'submitted' => esc_html__('Score submitted by', 'arcane'));
3692
3693 if(isset($_GET['add'])) {
3694 $this->add_notice(esc_html__('Match is successfully added.', 'arcane'), 'updated');
3695 }
3696
3697 if(isset($_GET['delete'])) {
3698 $deleted = (int)$_GET['delete'];
3699 $this->add_notice(sprintf(_n('%d Match deleted.', '%d Matches deleted', $deleted, 'arcane'), $deleted), 'updated');
3700 }
3701
3702 $this->print_notices();
3703
3704 ?>
3705 <div class="wrap wp-cw-matches">
3706 <h2><?php esc_html_e('Matches', 'arcane'); ?> <a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-matches&act=add')); ?>" class="add-new-h2"><?php esc_html_e('Add New', 'arcane'); ?></a></h2>
3707
3708 <div id="poststuff" class="metabox-holder">
3709
3710 <div id="post-body">
3711 <div id="post-body-content" class="has-sidebar-content">
3712
3713 <form id="posts-filter" >
3714 <div class="alignright actions">
3715 <label class="screen-reader-text" for="matches-search-input"><?php esc_html_e('Search Matches:', 'arcane'); ?></label>
3716 <input id="matches-search-input" name="s" value="<?php if(isset($search)) echo esc_html($search); ?>" type="text" onkeypress="GoSubmit(event);" />
3717
3718 <input id="matches-search-submit" value="<?php esc_html_e('Search Matches', 'arcane'); ?>" class="button" type="button" />
3719 <input name="post_type" class="post_type_matches" value="matches" type="hidden">
3720
3721 </div>
3722 </form>
3723 <form id="wp-teamwars-manageform" action="admin-post.php" method="post">
3724 <?php wp_nonce_field('wp-teamwars-deletematches'); ?>
3725
3726 <input type="hidden" name="action" value="wp-teamwars-deletematches" />
3727
3728 <div class="tablenav">
3729
3730 <div class="alignleft actions">
3731 <select name="do_action">
3732 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
3733 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
3734 </select>
3735 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction" id="wp-teamwars-doaction" class="button-secondary action" />
3736 </div>
3737
3738
3739
3740 <br class="clear" />
3741
3742 </div>
3743
3744 <div class="clear"></div>
3745
3746 <table class="widefat fixed" cellspacing="0">
3747 <thead>
3748 <tr>
3749 <?php $this->print_table_header($table_columns); ?>
3750 </tr>
3751 </thead>
3752
3753 <tfoot>
3754 <tr>
3755 <?php $this->print_table_header($table_columns, false); ?>
3756 </tr>
3757 </tfoot>
3758
3759 <tbody>
3760
3761 <?php foreach($matches as $i => $item) : ?>
3762
3763 <?php
3764 // if the match has no title so set default one
3765
3766 $pid = $item->ID;
3767 //$meta = arcane_get_meta($item->ID);
3768 $item->title = $item->post_title;
3769 $tparticipants = $item->tournament_participants;
3770
3771 $tid = 0;
3772 if(isset($item->tournament_id))
3773 $tid = $item->tournament_id;
3774
3775
3776 if (strpos(strtolower($tparticipants), 'user') === false) {
3777 $is_user_type = false;
3778 if (isset($item->team1_title) && strlen($item->team1_title) < 3) {
3779 if ($item->team1 > 0) {
3780 $item->team1_title = get_the_title($item->team1);
3781 $item->team1_logo = esc_url(arcane_return_team_image_big($item->team1));
3782 }
3783 }
3784 if (isset($item->team2_title) && strlen($item->team2_title) < 3) {
3785 if ($item->team2 > 0) {
3786 $item->team2_title = get_the_title($item->team2);
3787 $item->team2_logo = esc_url(arcane_return_team_image_big($item->team2));
3788 }
3789 }
3790 } else {
3791 $is_user_type = true;
3792 }
3793 if(empty($item->post_title))
3794 $item->post_title = esc_html__('Regular match', 'arcane');
3795
3796
3797 if (filter_has_var(INPUT_SERVER, "REQUEST_URI")) {
3798 $requesturi = filter_input(INPUT_SERVER, "REQUEST_URI", FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
3799 } else {
3800 if (isset($_SERVER["REQUEST_URI"]))
3801 $requesturi = filter_var($_SERVER["REQUEST_URI"], FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
3802 else
3803 $requesturi = null;
3804 }
3805 ?>
3806
3807 <tr class="iedit<?php if($i % 2 == 0) echo ' alternate'; ?>">
3808 <th scope="row" class="check-column"><input type="checkbox" name="delete[]" value="<?php echo esc_attr($item->ID); ?>" /></th>
3809 <td class="title column-title">
3810 <a class="row-title" href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-matches&act=edit&id=' . $item->ID)); ?>" title="<?php echo sprintf(esc_html__('Edit “%s” Match', 'arcane'), esc_attr($item->post_title)); ?>"><?php echo esc_html($item->post_title); ?></a><br />
3811 <div class="row-actions">
3812 <span class="edit"><a href="<?php echo esc_url(admin_url('admin.php?page=wp-teamwars-matches&act=edit&id=' . $item->ID)); ?>"><?php esc_html_e('Edit', 'arcane'); ?></a></span> | <span class="delete">
3813 <a href="<?php echo wp_nonce_url('admin-post.php?action=wp-teamwars-deletematches&do_action=delete&delete[]=' . $item->ID . '&_wp_http_referer=' . urlencode($requesturi), 'wp-teamwars-deletematches'); ?>"><?php esc_html_e('Delete', 'arcane'); ?></a></span>
3814 </div>
3815 </td>
3816 <td class="game_title column-game_title">
3817 <?php
3818 $game_icon = '';
3819 if(isset($item->game_icon))
3820 $game_icon = arcane_return_game_image($item->game_id);
3821
3822 if($game_icon !== false) :
3823 ?>
3824 <img src="<?php echo esc_url($game_icon); ?>" alt="<?php if(isset($item->game_title))echo esc_attr($item->game_title); ?>" class="icon" />
3825 <?php endif; ?>
3826
3827 </td>
3828 <td class="date column-date">
3829 <?php
3830 if(isset($item->date_unix) && !empty($item->date_unix)){
3831 $date = date_i18n(get_option('date_format') . ', ' . get_option('time_format'), $item->date_unix);
3832
3833 }else{
3834 $date = mysql2date(get_option('date_format') . ', ' . get_option('time_format'), $item->date);
3835
3836 }
3837 echo esc_attr($date); ?>
3838 </td>
3839 <td class="match_status column-match_status">
3840 <?php
3841 $n = $item->match_status;
3842
3843 if(isset($this->match_status[$n]) && $tid == 0)
3844 echo esc_attr($this->match_status[$n]);
3845 ?>
3846 </td>
3847 <td class="team1 column-team1">
3848 <?php
3849 if ($is_user_type == false) {
3850 if(isset($item->team1))
3851 echo esc_html(arcane_return_team_name_by_team_id($item->team1));
3852 } else {
3853 $user1 = get_user_by('id', $item->team1);
3854 if(isset($user1->display_name))
3855 echo esc_html($user1->display_name);
3856 }
3857
3858
3859 ?>
3860 </td>
3861 <td class="team2 column-team2">
3862 <?php
3863 if ($is_user_type == false) {
3864 if(isset($item->team2))
3865 echo esc_html(arcane_return_team_name_by_team_id($item->team2));
3866 } else {
3867 $user2 = get_user_by('id', $item->team2);
3868 if(isset($user2->display_name))
3869 echo esc_html($user2->display_name);
3870 } ?>
3871 </td>
3872 <td class="tickets column-tickets">
3873 <?php if(!isset($item->team1_tickets)) $item->team1_tickets = 0; ?>
3874 <?php if(!isset($item->team2_tickets)) $item->team2_tickets = 0; ?>
3875 <?php echo sprintf('%s:%s', $item->team1_tickets, $item->team2_tickets); ?>
3876 </td>
3877 <td class="reported column-reported">
3878 <?php if($item->status == 'reported1' or $item->status == 'reported2'){ ?><i class="fas fa-exclamation-triangle"></i> <?php esc_html_e('Reported','arcane'); ?><?php } ?>
3879 </td>
3880 <td class="match_status column-match_status">
3881 <?php if($item->locked == 1){ esc_html_e('Yes', 'arcane');}else{esc_html_e('No', 'arcane'); } ?>
3882 </td>
3883
3884 <td class="match_status column-match_status">
3885
3886 <?php if($item->status == 'submitted1'){
3887 if ($is_user_type == false) {
3888 echo esc_html($item->team1_title);
3889 } else {
3890 $user = get_user_by('id', $item->team1);
3891 echo esc_html($user->display_name);
3892 }
3893
3894 }elseif($item->status == 'submitted2'){
3895 if ($is_user_type == false) {
3896 echo esc_html(get_the_title($item->team2));
3897 } else {
3898 $user2 = get_user_by('id', $item->team2);
3899 echo esc_html($user2->display_name);
3900 }
3901 } ?>
3902 </td>
3903 </tr>
3904
3905 <?php endforeach; ?>
3906
3907 </tbody>
3908
3909 </table>
3910
3911 <div class="tablenav">
3912
3913 <div class="tablenav-pages"><?php echo wp_kses($page_links_text,$arcane_allowed); ?></div>
3914
3915 <div class="alignleft actions">
3916 <select name="do_action2">
3917 <option value="" selected="selected"><?php esc_html_e('Bulk Actions', 'arcane'); ?></option>
3918 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
3919 </select>
3920 <input type="submit" value="<?php esc_html_e('Apply', 'arcane'); ?>" name="doaction2" id="wp-teamwars-doaction2" class="button-secondary action" />
3921 </div>
3922
3923 <br class="clear" />
3924
3925 </div>
3926
3927 </form>
3928
3929 </div>
3930 </div>
3931 <br class="clear"/>
3932
3933 </div>
3934 </div>
3935 <?php
3936 }
3937
3938 function on_admin_post_settings() {
3939 global $wpdb;
3940
3941 if(!current_user_can('manage_options'))
3942 wp_die(esc_html__('Cheatin’ uh?', 'arcane'));
3943
3944 check_admin_referer('wp-teamwars-settings');
3945
3946 if(isset($_POST['category']))
3947 update_option(ARCANE_TEAMWARS_CATEGORY, (int)$_POST['category']);
3948
3949 update_option(ARCANE_TEAMWARS_DEFAULTCSS, isset($_POST['enable_default_styles']));
3950
3951 $url = add_query_arg('saved', 'true', $_POST['_wp_http_referer']);
3952
3953 wp_redirect($url, $status = 302);
3954 }
3955
3956 function on_admin_post_acl() {
3957 global $wpdb;
3958
3959 if(!current_user_can('manage_options'))
3960 wp_die(esc_html__('Cheatin’ uh?', 'arcane'));
3961
3962 check_admin_referer('wp-teamwars-acl');
3963
3964 if(isset($_POST['user'])) {
3965 $user_id = (int)$_POST['user'];
3966 $data = array();
3967
3968 if(isset($_POST['permissions']))
3969 $data['permissions'] = $_POST['permissions'];
3970
3971 if(isset($_POST['games']))
3972 $data['games'] = $_POST['games'];
3973
3974 $this->acl_update($user_id, $data);
3975 }
3976
3977 $url = add_query_arg('saved', 'true', $_POST['_wp_http_referer']);
3978
3979 wp_redirect($url, $status = 302);
3980 }
3981
3982 function on_admin_post_deleteacl() {
3983 global $wpdb;
3984
3985 if(!current_user_can('manage_options'))
3986 wp_die(esc_html__('Cheatin’ uh?', 'arcane'));
3987
3988 check_admin_referer('wp-teamwars-deleteacl');
3989
3990 extract($this->extract_args($_POST, array(
3991 'doaction' => '', 'doaction2' => '',
3992 'users' => array()
3993 )));
3994
3995 $url = $_POST['_wp_http_referer'];
3996
3997 if($doaction == 'delete' || $doaction2 == 'delete') {
3998
3999 $users = array_unique(array_values($users));
4000
4001 foreach($users as $key => $user_id)
4002 $this->acl_delete($user_id);
4003
4004 $url = add_query_arg('saved', 'true', $url);
4005 }
4006
4007 wp_redirect($url, $status = 302);
4008 }
4009
4010 function on_admin_post_import() {
4011 if(!current_user_can('manage_options'))
4012 wp_die(esc_html__('Cheatin’ uh?', 'arcane'));
4013
4014 check_admin_referer('wp-teamwars-import');
4015
4016 extract($this->extract_args($_POST, array('import' => '', 'items' => array())));
4017
4018 $url = remove_query_arg(array('upload', 'import'), $_POST['_wp_http_referer']);
4019
4020 switch($import) {
4021 case 'upload':
4022 if(isset($_FILES['userfile'])) {
4023 $file = $_FILES['userfile'];
4024
4025 if($file['error'] == 0) {
4026 $content = $this->_get_file_content($file['tmp_name']);
4027
4028 $result = $this->import_games($content);
4029 $url = add_query_arg('import', $result, $url);
4030 } else {
4031 $url = add_query_arg('upload', 'error', $url);
4032 }
4033 }
4034 break;
4035
4036 case 'available':
4037
4038 $available_games = $this->get_available_games();
4039
4040 foreach($items as $item) {
4041 if(isset($available_games[$item])) {
4042 $r = $available_games[$item];
4043
4044 $content = $this->_get_file_content(trailingslashit(get_parent_theme_file_path('addons/team-wars/import')) . $r->package);
4045 $this->import_games($content);
4046 }
4047 }
4048
4049 $url = add_query_arg('import', true, $url);
4050
4051 break;
4052 }
4053
4054 wp_redirect($url, $status = 302);
4055 }
4056
4057 function on_settings() {
4058
4059 $table_columns = array('cb' => '<input type="checkbox" />',
4060 'user_login' => esc_html__('User Login', 'arcane'),
4061 'user_permissions' => esc_html__('Permissions', 'arcane')
4062 );
4063
4064 $games = $this->get_game('id=all');
4065 if(count($games) > 1){
4066 $obj = new stdClass();
4067 $obj->id = 0;
4068 $obj->title = esc_html__('All', 'arcane');
4069 $obj->abbr = esc_html__('All', 'arcane');
4070 $obj->icon = 0;
4071
4072 array_unshift($games, $obj);
4073 }
4074
4075 ?>
4076 <div class="wrap wp-cw-settings">
4077
4078 <h2><?php esc_html_e('Settings', 'arcane'); ?></h2>
4079
4080 <?php if(isset($_GET['saved'])) : ?>
4081 <div class="updated fade"><p><?php esc_html_e('Settings saved.', 'arcane'); ?></p></div>
4082 <?php endif; ?>
4083
4084 <form method="post" action="admin-post.php">
4085 <?php wp_nonce_field('wp-teamwars-settings'); ?>
4086 <input type="hidden" name="action" value="wp-teamwars-settings" />
4087
4088 <table class="form-table">
4089 <tr valign="top">
4090 <th scope="row"><?php esc_html_e('Matches Category', 'arcane'); ?></th>
4091 <td>
4092 <?php
4093
4094 $selected = get_option(ARCANE_TEAMWARS_CATEGORY, -1);
4095
4096 wp_dropdown_categories(
4097 array('name' => 'category',
4098 'hierarchical' => true,
4099 'show_option_none' => esc_html__('None', 'arcane'),
4100 'hide_empty' => 0,
4101 'hide_if_empty' => 0,
4102 'selected' => $selected)
4103 );
4104
4105 ?>
4106 </td>
4107 </tr>
4108 <tr valign="top">
4109 <th scope="row"><?php esc_html_e('Enable default styles', 'arcane'); ?></th>
4110 <td><input type="checkbox" name="enable_default_styles" value="true"<?php checked(get_option(ARCANE_TEAMWARS_DEFAULTCSS), true); ?> /></td>
4111 </tr>
4112 </table>
4113
4114 <p class="submit">
4115 <input class="button-secondary" value="<?php esc_html_e('Save Changes', 'arcane'); ?>" type="submit" />
4116 </p>
4117
4118 </form>
4119
4120 <h2><?php esc_html_e('User Access', 'arcane'); ?></h2>
4121
4122 <div id="col-container">
4123
4124 <div id="col-right">
4125 <div class="col-wrap">
4126
4127 <form method="post" action="admin-post.php">
4128 <?php wp_nonce_field('wp-teamwars-deleteacl'); ?>
4129 <input type="hidden" name="action" value="wp-teamwars-deleteacl" />
4130
4131 <div class="tablenav">
4132 <div class="alignleft actions">
4133 <select name="doaction">
4134 <option value="" selected="selected"><?php esc_html_e('Actions', 'arcane'); ?></option>
4135 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
4136 </select>
4137 <input value="<?php esc_html_e('Apply', 'arcane'); ?>" class="button-secondary action" type="submit" />
4138 </div>
4139 <br class="clear" />
4140 </div>
4141
4142
4143 <table class="widefat fixed" cellspacing="0">
4144 <thead>
4145 <tr>
4146 <?php $this->print_table_header($table_columns); ?>
4147 </tr>
4148 </thead>
4149
4150 <tfoot>
4151 <tr>
4152 <?php $this->print_table_header($table_columns, false); ?>
4153 </tr>
4154 </tfoot>
4155
4156 <tbody>
4157 <?php
4158
4159 $acl = $this->acl_get();
4160
4161 $keys = array_keys($acl);
4162
4163 for($i = 0; $i < sizeof($keys); $i++) :
4164
4165 $user_id = $keys[$i];
4166 $user_acl = $acl[$user_id];
4167 $user = get_userdata($user_id);
4168
4169 ?>
4170
4171 <tr<?php if($i % 2 == 0) : ?> class="alternate"<?php endif; ?>>
4172 <th class="check-column"><input type="checkbox" class="check" name="users[]" value="<?php echo esc_attr($user_id); ?>" /></th>
4173 <td><?php echo esc_attr($user->user_login); ?></td>
4174 <td>
4175 <?php foreach($user_acl['permissions'] as $name => $is_allowed) : ?>
4176 <ul>
4177 <li><?php echo esc_attr($this->acl_keys[$name]); ?>: <?php echo ($is_allowed) ? esc_html__('Yes', 'arcane') : esc_html__('No', 'arcane'); ?></li>
4178 </ul>
4179 <?php endforeach; ?>
4180
4181 <?php
4182 $allowed_games = $this->acl_user_can('which_games', false, $user_id);
4183 $user_games = $this->get_game(array('id' => $allowed_games, 'orderby' => 'title', 'order' => 'asc'));
4184
4185 if($allowed_games == 'all') {
4186 echo esc_html__('All', 'arcane');
4187 }
4188 ?>
4189
4190 <?php foreach($user_games as $game) :
4191
4192 $game_icon = wp_get_attachment_url($game->icon);
4193
4194 if($game_icon !== false) {
4195 echo '<img src="' . $game_icon . '" alt="' . esc_attr($game->title) . '" class="icon" /> ';
4196 } else {
4197 echo esc_html(empty($game->abbr) ? $game->title : $game->abbr);
4198 }
4199
4200 endforeach; ?>
4201 </td>
4202 </tr>
4203
4204 <?php endfor; ?>
4205 </tbody>
4206 </table>
4207
4208 <div class="tablenav">
4209 <div class="alignleft actions">
4210 <select name="doaction2">
4211 <option value="" selected="selected"><?php esc_html_e('Actions', 'arcane'); ?></option>
4212 <option value="delete"><?php esc_html_e('Delete', 'arcane'); ?></option>
4213 </select>
4214 <input value="<?php esc_html_e('Apply', 'arcane'); ?>" class="button-secondary action" type="submit" />
4215 </div>
4216 <br class="clear" />
4217 </div>
4218
4219 </form>
4220
4221 </div></div>
4222
4223 <div id="col-left">
4224 <div class="col-wrap">
4225
4226 <h3><?php esc_html_e('Add New User', 'arcane'); ?></h3>
4227
4228 <form class="form-wrap" method="post" action="admin-post.php">
4229 <?php wp_nonce_field('wp-teamwars-acl'); ?>
4230 <input type="hidden" name="action" value="wp-teamwars-acl" />
4231
4232 <div class="form-field">
4233 <label for="user"><?php esc_html_e('User', 'arcane'); ?></label>
4234 <?php wp_dropdown_users('name=user'); ?>
4235 </div>
4236
4237 <div class="form-field">
4238 <label><?php esc_html_e('Allow user manage specified games only:', 'arcane'); ?></label>
4239 <ul class="listbox">
4240 <?php foreach($games as $g) : ?>
4241 <li><label for="game_<?php echo esc_attr($g->id); ?>"><input type="checkbox" name="games[]" id="game_<?php echo esc_attr($g->id); ?>" value="<?php echo esc_attr($g->id); ?>" /> <?php echo esc_html($g->title); ?></label></li>
4242 <?php endforeach; ?>
4243 </ul>
4244
4245 <p class="description"><?php esc_html_e('User can create new games <strong>only if “All” option is checked.</strong>', 'arcane'); ?></p>
4246 </div>
4247
4248 <div class="form-field">
4249 <label><?php esc_html_e('Allow user:', 'arcane'); ?></label>
4250 <ul class="listbox">
4251 <?php foreach($this->acl_keys as $key => $title) : ?>
4252 <li><label for="<?php echo esc_attr($key); ?>"><input type="checkbox" class="check" name="permissions[<?php echo esc_attr($key); ?>]" value="1" id="<?php echo esc_attr($key); ?>" /> <?php echo esc_attr($title); ?></label></li>
4253 <?php endforeach; ?>
4254
4255 </ul>
4256 </div>
4257
4258 <p class="submit">
4259 <input type="submit" class="button-secondary" value="<?php esc_html_e('Add User', 'arcane'); ?>" />
4260 </p>
4261 </form>
4262
4263 </div></div>
4264
4265 </div>
4266
4267 </div>
4268
4269 <?php
4270
4271 }
4272
4273 function get_available_games() {
4274 $content = $this->_get_file_content(trailingslashit(get_parent_theme_file_path('addons/team-wars/import')) . 'import.json');
4275
4276 if($content)
4277 return json_decode($content);
4278
4279 return false;
4280 }
4281
4282 function is_game_installed($title, $abbr = '', $objects = false) {
4283
4284 if(!is_array($objects))
4285 $objects = $this->get_game('');
4286
4287 foreach($objects as $p) {
4288 if(preg_match('#' . preg_quote($abbr, '#') . '#i', $p->abbr))
4289 return $p;
4290
4291 if(preg_match('#' . preg_quote($title, '#') . '#i', $p->title))
4292 return $p;
4293 }
4294
4295 return false;
4296 }
4297
4298 function on_import() {
4299
4300 $import_list = $this->get_available_games();
4301 $installed_games = $this->get_game('');
4302
4303 if(isset($_GET['upload']))
4304 $this->add_notice(esc_html__('An upload error occurred while import.', 'arcane'), 'error');
4305
4306 if(isset($_GET['import']))
4307 $this->add_notice($_GET['import'] ? esc_html__('File successfully imported.', 'arcane') : esc_html__('An error occurred while import.', 'arcane'), $_GET['import'] ? 'updated' : 'error');
4308
4309 echo esc_attr($this->print_notices());
4310
4311 ?>
4312 <div class="wrap">
4313 <div id="icon-tools" class="icon32"><br></div>
4314 <h2><?php esc_html_e('Import games', 'arcane'); ?></h2>
4315
4316 <form id="wp-cw-import" method="post" action="admin-post.php" enctype="multipart/form-data">
4317
4318
4319 <input type="hidden" name="action" value="wp-teamwars-import" />
4320 <?php wp_nonce_field('wp-teamwars-import'); ?>
4321
4322
4323 <p><label for="upload"><input type="radio" name="import" id="upload" value="upload" checked="checked" /> <?php esc_html_e('Upload Package (gz file)', 'arcane'); ?></label></p>
4324
4325 <p><input type="file" name="userfile" /></p>
4326
4327 <?php if(!empty($import_list)) : ?>
4328
4329 <p><label for="available"><input type="radio" name="import" id="available" value="available" /> <?php esc_html_e('Import Available Packages', 'arcane'); ?></label></p>
4330
4331 <ul class="available-games">
4332
4333 <?php foreach($import_list as $index => $game) :
4334
4335 $installed = $this->is_game_installed($game->title, $game->abbr, $installed_games);
4336
4337 ?>
4338
4339 <li>
4340 <label for="game-<?php echo esc_attr($index); ?>">
4341 <input type="checkbox" name="items[]" id="game-<?php echo esc_attr($index); ?>" value="<?php echo esc_attr($index); ?>" /> <img src="<?php echo esc_attr(trailingslashit(get_theme_file_uri('addons/team-wars/import')) . $game->icon); ?>" alt="<?php echo esc_attr($game->title); ?>" /> <?php echo esc_html($game->title); ?>
4342 <?php if($installed !== false) : ?>
4343 <span class="description"><?php esc_html_e('installed', 'arcane'); ?></span>
4344 <?php endif; ?>
4345 </label>
4346 </li>
4347
4348 <?php endforeach; ?>
4349
4350 </ul>
4351
4352 <?php endif; ?>
4353
4354 <p class="submit"><input type="submit" class="button-secondary" value="<?php esc_html_e('Import', 'arcane'); ?>" /></p>
4355
4356 </form>
4357
4358 </div>
4359 <?php
4360
4361 }
4362
4363}
4364
4365/*
4366 * Initialization
4367 */
4368
4369$ArcaneWpTeamWars = new Arcane_TeamWars();
4370add_action('init', array(&$ArcaneWpTeamWars, 'on_init'));
4371
4372
4373?>