-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesport-scoreboard.php
176 lines (127 loc) · 4.69 KB
/
esport-scoreboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*
Plugin Name: eSports ScoreBoard
Plugin URI: http://www.alpenscene.at
Description: Create and manage teams and their matches and view their results.
Version: 0.1
Author: Mario Neuhold
Plugin Type: Piklist
License: GPL2
*/
require_once 'includes/essb_post_types.php';
if( ! class_exists('ScoreBoard') ) :
class eSportScoreBoard {
function __construct() {
essb_post_types();
$this->autofill_match_title();
add_action( 'save_post', array(__CLASS__, 'autofill_match_permalink'));
add_action( 'admin_enqueue_scripts', function() {
wp_enqueue_style( 'essb_datetimepicker_css', plugins_url('/resources/lib/xdan-datetimepicker/jquery.datetimepicker.css', __FILE__),false,'0.2','all');
// datetimepicker.full.min is required to prevent i.dateformat error
wp_enqueue_script( 'essb_datetimepicker_js', plugins_url('/resources/lib/xdan-datetimepicker/jquery.datetimepicker.full.min.js', __FILE__), array( 'jquery' ), '20161129', true);
wp_enqueue_script( 'essb_admin_js', plugins_url('/resources/js/essb.js', __FILE__), array( 'jquery' ), '20161129', true);
} );
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'essb_css', plugins_url('/resources/css/esport-scoreboard.css', __FILE__),false,'0.3','all');
wp_enqueue_script( 'essb_admin_js', plugins_url('/resources/js/essb.js', __FILE__), array( 'jquery' ), '20161129', true);
} );
/**
* Set the Archive title
*/
add_filter( 'get_the_archive_title', function ( $title ) {
if(is_post_type_archive('essb_match') ) {
$title = __('All Matches', 'esport_scoreboard');
}
return $title;
});
/**
* View all matches on the archive page
*/
add_action('pre_get_posts', function($query) {
if ($query->is_archive() && is_post_type_archive('essb_match')) {
$query->set('posts_per_page', -1);
return $query;
}
});
}
/**
* Calls a rest-api for getting all countries for an area (continent)
* @see https://restcountries.eu/
* @param string $area Default 'Europe'
* @return array $countriesByArea ['at' => 'Austria', 'de' => 'Germany']
*/
public static function getCountries($area = 'Europe') {
$countryurl = 'https://restcountries.eu/rest/v1/all';
// Initialize a single cURL handle for use of each online-check
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $countryurl
));
$countries = json_decode(curl_exec($ch));
// Close the curl request
curl_close($ch);
foreach ($countries as $country) {
if ($area !== 'all' && $country->region === $area) {
$countriesByArea[strtolower($country->alpha2Code)] = $country->name;
} elseif ($area === 'all') {
$countriesByArea[strtolower($country->alpha2Code)] = $country->name;
}
}
return $countriesByArea;
}
/**
* Hooking into title_save_pre for our Match CPT so it get's a proper name automatically
* @see https://www.iftekhar.net/auto-generate-post-title-for-posts-or-custom-post-type-in-wordpress/
* @todo disable title input in dashboard
*/
public static function autofill_match_title() {
add_filter('title_save_pre', function($title) {
global $post;
if (isset($post->ID) && get_post_type($post->ID) == 'essb_match') {
// get the current post ID number
$id = get_the_ID();
// add ID number with order strong
$title = $id . ' - ' . get_post(get_post_meta($id, 'match_team1', true))->team_tag . ' vs ' . get_post(get_post_meta($id, 'match_team2', true))->team_tag;
}
return $title;
});
// @see http://stackoverflow.com/a/10017619
add_action('admin_head', 'modify_admin_css');
function modify_admin_css() {
global $post;
if (isset($post->ID) && get_post_type($post->ID) == 'essb_match') {
?>
<style>
div#titlediv {
display: none;
}
</style>
<?php
}
}
}
// http://wordpress.stackexchange.com/a/105936
public static function autofill_match_permalink($post_id) {
// verify post is match and not a revision
if ( get_post_type($post_id) == 'essb_match' && ! wp_is_post_revision( $post_id ) ) {
// unhook this function to prevent infinite looping
remove_action( 'save_post', array(__CLASS__, 'autofill_match_permalink') );
// update the post slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => sanitize_title(get_the_title($post_id))
));
// re-hook this function
add_action( 'save_post', array(__CLASS__, 'autofill_match_permalink') );
}
}
}
endif;
global $scoreboard;
if( !isset($scoreboard) ) {
$scoreboard = new eSportScoreBoard();
}