forked from samperrow/pre-party-browser-hints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gktpp-insert-to-db.php
159 lines (130 loc) · 4.32 KB
/
class-gktpp-insert-to-db.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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class GKTPP_Insert_To_DB {
public $as_attr = '';
public $type_attr = '';
public $crossorigin = '';
public $url = '';
public $header_str = '';
public $head_str = '';
public function insert_data_to_db() {
if ( ! is_admin() ) {
exit;
}
global $wpdb;
$table = $wpdb->prefix . 'gktpp_table';
$hint_type = isset( $_POST['hint_type'] ) ? stripslashes( $_POST['hint_type'] ) : '';
isset( $_POST['url'] ) ? $this->configure_hint_attrs( self::santize_url( $_POST['url'] ), $hint_type ) : '';
$this->create_str( $this->url, $hint_type, $this->as_attr, $this->type_attr, $this->crossorigin );
$sql = "INSERT INTO $table ( id, url, hint_type, status, as_attr, type_attr, crossorigin, ajax_domain, header_string, head_string )
VALUES ( null, %s, %s, 'Enabled', %s, %s, %s, %d, %s, %s )";
$wpdb->query(
$wpdb->prepare( $sql,
array( $this->url, $hint_type, $this->as_attr, $this->type_attr, $this->crossorigin, 0, $this->header_str, $this->head_str) ) );
}
private static function santize_url( $url ) {
return esc_url( preg_replace('/[^A-z0-9?=\.\/\-:\s]/', '', $url) );
}
public function create_str( $url, $hint_type, $as_attr, $type_attr, $crossorigin ) {
$hint_type = strtolower( $hint_type );
$header_as_attr = $header_type_attr = $head_as_attr = $head_type_attr = $header_crossorigin = $head_crossorigin = '';
if ( strlen($as_attr) > 0 ) {
$header_as_attr = " as=$as_attr;";
$head_as_attr = " as='$as_attr'";
}
if ( strlen($type_attr) > 0 ) {
$header_type_attr = " type=$type_attr;";
$head_type_attr = " type='$type_attr'";
}
if ( strlen($crossorigin) > 0 ) {
$header_crossorigin = " $crossorigin;";
$head_crossorigin = " $crossorigin";
}
$this->head_str = "<link href='$url' rel='$hint_type'$head_as_attr$head_type_attr$head_crossorigin>";
$header = "<$url>; rel=$hint_type;$header_as_attr$header_type_attr$header_crossorigin";
if ( strrpos( $header, ';') === (strlen( $header) - 1) ) { // replace the last semi-colon and replace it with a comma.
$header = substr( $header, 0, strrpos( $header, ';') ) . ',';
}
return $this->header_str = $header;
}
public function get_attributes( $url ) {
$basename = pathinfo( $url )['basename'];
$file_type = strlen( strpbrk( $basename, '?' ) ) > 0
? strrchr( explode( '?', $basename )[0], '.' )
: strrchr( $basename, '.' );
switch ( $file_type ) {
case '.js':
$this->as_attr = 'script';
break;
case '.css':
$this->as_attr= 'style';
break;
case '.mp3':
$this->as_attr = 'audio';
break;
case '.mp4':
$this->as_attr = 'video';
break;
case '.jpg':
case '.jpeg':
case '.png':
case '.svg':
case '.webp':
$this->as_attr = 'image';
break;
case '.vtt':
$this->as_attr = 'track';
break;
case '.woff':
$this->as_attr = 'font';
$this->crossorigin = 'crossorigin';
$this->type_attr = 'font/woff';
break;
case '.woff2':
$this->as_attr = 'font';
$this->crossorigin = 'crossorigin';
$this->type_attr = 'font/woff2';
break;
case '.ttf':
$this->as_attr = 'font';
$this->crossorigin = 'crossorigin';
$this->type_attr = 'font/ttf';
break;
case '.eot':
$this->as_attr = 'font';
$this->crossorigin = 'crossorigin';
$this->type_attr = 'font/eot';
break;
case '.swf':
$this->as_attr = 'embed';
break;
default:
$this->as_attr = '';
}
if ( strlen($this->crossorigin) === 0) {
$this->check_for_crossorigin( $url );
}
return $this->as_attr;
}
public function check_for_crossorigin( $url ) {
return $this->crossorigin = preg_match( '/(fonts.googleapis.com|fonts.gstatic.com)/i', $url ) ? 'crossorigin' : '';
}
private function configure_hint_attrs( $url, $hint_type ) {
$this->get_attributes( $url );
if ( $hint_type === 'DNS-Prefetch' || $hint_type === 'Preconnect' ) {
return $this->filter_for_domain_name( $url );
}
return $this->url = $url;
}
private function filter_for_domain_name( $url ) {
if ( preg_match( '/(http|https)/i', $url ) ) {
return $this->url = parse_url( $url, PHP_URL_SCHEME ) . '://' . parse_url( $url, PHP_URL_HOST );
} elseif ( substr( $url, 0, 2 ) === '//' ) {
return $this->url = '//' . parse_url( $url, PHP_URL_HOST );
} else {
return $this->url = '//' . parse_url( $url, PHP_URL_PATH );
}
}
}