1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Perform fetch insight action |
6
|
|
|
*/ |
7
|
|
|
if ( ! class_exists( 'CGSS_INSIGHT' ) ) { |
8
|
|
|
|
9
|
|
|
final class CGSS_INSIGHT { |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
public function __construct() { |
13
|
|
|
|
14
|
|
|
$this->data = $this->fetch(); |
|
|
|
|
15
|
|
|
$this->compile(); |
16
|
|
|
$this->save(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
//DB insert function |
21
|
|
|
public function save() { |
22
|
|
|
|
23
|
|
|
global $wpdb; |
24
|
|
|
|
25
|
|
|
$result = array( |
26
|
|
|
$this->score, |
27
|
|
|
$this->snippet, |
28
|
|
|
$this->text, |
29
|
|
|
$this->links, |
30
|
|
|
$this->keywords, |
31
|
|
|
$this->images, |
32
|
|
|
$this->responsive, |
33
|
|
|
$this->speed, |
34
|
|
|
$this->social, |
35
|
|
|
); |
36
|
|
|
foreach ($result as $key => $value) { |
37
|
|
|
$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}cgss_insight SET remark = %s WHERE ID = %d", $value, ($key+1)); |
38
|
|
|
$update = $wpdb->query($sql); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
//Compile the result |
44
|
|
|
public function compile() { |
45
|
|
|
|
46
|
|
|
$score = $this->data['score']; |
47
|
|
|
$this->count = count($score); |
|
|
|
|
48
|
|
|
$avarage_score = round(array_sum($score)/count($score), 0); |
49
|
|
|
|
50
|
|
|
$this->score = sprintf(__('Avarage SEO score is %d', 'cgss'),$avarage_score); |
|
|
|
|
51
|
|
|
$this->snippet = $this->snippet_analyze(); |
|
|
|
|
52
|
|
|
$this->text = $this->text_analyze(); |
|
|
|
|
53
|
|
|
$this->links = $this->link_analyze(); |
|
|
|
|
54
|
|
|
$this->keywords = $this->keyword_analyze(); |
|
|
|
|
55
|
|
|
$this->images = $this->image_analyze(); |
|
|
|
|
56
|
|
|
$this->responsive = $this->resposivity(); |
|
|
|
|
57
|
|
|
$this->speed = $this->speed_analyze(); |
|
|
|
|
58
|
|
|
$this->social = $this->social_analyze(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
// Analysis of links |
64
|
|
|
public function link_analyze() { |
65
|
|
|
|
66
|
|
|
$text = $this->data['text']; |
67
|
|
|
$links = array_column($text, 'links'); |
68
|
|
|
$count = round(array_sum(array_column($links, 'count')) / $this->count, 0); |
69
|
|
|
$external = round(array_sum(array_column($links, 'external')) / $this->count, 0); |
70
|
|
|
$nofollow = round(array_sum(array_column($links, 'external')) / $this->count, 0); |
71
|
|
|
$external_percentage = round(($external/$count)*100, 0); |
72
|
|
|
$nofollow_percentage = round(($nofollow/$count)*100, 0); |
73
|
|
|
|
74
|
|
|
$output = sprintf(__('Avarage', 'cgss') . ' '. _n( '%d link','%d links', $count, 'cgss' ) . ' ' . __('are found per page and %d percent are external and %d percent are nofollow among them.', 'cgss'),$count,$external_percentage,$nofollow_percentage); |
|
|
|
|
75
|
|
|
return $output; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
// Analyze keywords |
80
|
|
|
public function keyword_analyze() { |
81
|
|
|
|
82
|
|
|
$text = $this->data['text']; |
83
|
|
|
$keywords = array_column($text, 'keys'); |
84
|
|
|
|
85
|
|
|
$key_collect = array(); |
86
|
|
|
$percent_collect = array(); |
87
|
|
|
foreach ($keywords as $keyword) { |
88
|
|
|
$keys = array_keys($keyword); |
89
|
|
|
$top_key = $keys[0]; |
90
|
|
|
$key_collect[] = count(explode(' ', $top_key)); |
91
|
|
|
$percent_collect[] = $keyword[$top_key]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$key_count = round(array_sum($key_collect) / $this->count, 1); |
95
|
|
|
$percent = round(array_sum($percent_collect) / $this->count, 1); |
96
|
|
|
|
97
|
|
|
$output = sprintf(__('Avarage foucs keyword is', 'cgss') . ' ' . _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long with keywords frequency of %d percent','cgss'),$key_count,$percent); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $output; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
// Analyze images |
104
|
|
|
public function image_analyze() { |
105
|
|
|
|
106
|
|
|
$design = $this->data['design']; |
107
|
|
|
$images = array_column($design, 'image'); |
108
|
|
|
|
109
|
|
|
$image_count = array_sum(array_column($images, 'count')); |
110
|
|
|
$no_alt_image = array_sum(array_column($images, 'no_alt_count')); |
111
|
|
|
|
112
|
|
|
$avg_image = round(($image_count/$this->count), 0); |
113
|
|
|
|
114
|
|
|
$output = sprintf(__('Avarage', 'cgss') . ' ' . _n( '%d image', '%d images', $avg_image, 'cgss' ) . ' ' . __( 'are found per page and', 'cgss'),$avg_image) . ' '; |
|
|
|
|
115
|
|
|
if ($no_alt_image == 0) { |
116
|
|
|
$output .= __('all of them are optimized', 'cgss'); |
117
|
|
|
} else { |
118
|
|
|
$no_alt_percent = round(($no_alt_image/$image_count)*100, 0); |
119
|
|
|
$output .= sprintf(__('%d percent among them doesn\'t have alt tag', 'cgss'),$no_alt_percent); |
120
|
|
|
} |
121
|
|
|
return $output; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// Check mobile optimized |
127
|
|
|
public function resposivity() { |
128
|
|
|
|
129
|
|
|
$design = $this->data['design']; |
130
|
|
|
$vport = array_sum(array_column($design, 'vport')); |
131
|
|
|
|
132
|
|
|
if ($vport == $this->count) { |
133
|
|
|
$output = __('All pages are mobile optimized', 'cgss'); |
|
|
|
|
134
|
|
|
} else { |
135
|
|
|
$no_mobile_percent = round(($vport/$this->count)*100, 0); |
136
|
|
|
$output = sprintf(__('%d percent pages aren\'t mobile optimized', 'cgss'),$no_mobile_percent); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $output; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
// Speed analyze |
144
|
|
|
public function speed_analyze() { |
145
|
|
|
|
146
|
|
|
$speed = $this->data['speed']; |
147
|
|
|
$res_time = array_sum(array_column($speed, 'res_time')); |
148
|
|
|
$down_time = array_sum(array_column($speed, 'down_time')); |
149
|
|
|
|
150
|
|
|
$avg_res_time = round(($res_time/$this->count), 2); |
151
|
|
|
$avg_down_time = round(($down_time/$this->count), 2); |
152
|
|
|
|
153
|
|
|
$output = sprintf( __('Average response time is %d s and average download time is %d s', 'cgss'), $avg_res_time, $avg_down_time); |
|
|
|
|
154
|
|
|
|
155
|
|
|
return $output; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
// Count social shares |
160
|
|
|
public function social_analyze() { |
161
|
|
|
|
162
|
|
|
$social = $this->data['social']; |
163
|
|
|
|
164
|
|
|
$gplus = array_sum(array_column($social, 'gplus')); |
165
|
|
|
$fb_share = array_sum(array_column($social, 'fb_share')); |
166
|
|
|
$fb_like = array_sum(array_column($social, 'fb_like')); |
167
|
|
|
|
168
|
|
|
$output = sprintf( __('Total', 'cgss' ). ' ' . _n( '%d share', '%d shares', $gplus, 'cgss' ) . ' ' . __( 'in g+ and', 'cgss' ). ' ' . _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in FB and', 'cgss' ). ' ' . _n( '%d FB like', '%d FB likes', $fb_like, 'cgss' ) . ' ' . __( 'are present', 'cgss'), $gplus, $fb_share, $fb_like); |
|
|
|
|
169
|
|
|
|
170
|
|
|
return $output; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
//Analyze text |
175
|
|
|
public function text_analyze() { |
176
|
|
|
|
177
|
|
|
$text = $this->data['text']; |
178
|
|
|
$count = round(array_sum(array_column($text, 'count')) / $this->count, 0); |
179
|
|
|
$ratio = round(array_sum(array_column($text, 'ratio')) / $this->count, 2); |
180
|
|
|
|
181
|
|
|
$output = sprintf(__('Avarage', 'cgss') . ' ' . _n('%d word','%d words',$count,'cgss') . ' ' . __( 'are found per page and avarage text to HTML ratio is %d percent', 'cgss'),$count,$ratio); |
|
|
|
|
182
|
|
|
return $output; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
//Analyze snippets |
187
|
|
|
public function snippet_analyze() { |
188
|
|
|
|
189
|
|
|
$snippets = $this->data['snip']; |
190
|
|
|
$snip_count = 0; |
191
|
|
|
foreach ($snippets as $snippet) { |
192
|
|
|
$title = $snippet['title']; |
193
|
|
|
$desc = $snippet['desc']; |
194
|
|
|
if (!empty($title) && !empty($desc)) { |
195
|
|
|
$snip_count++; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
$snip_fraction = $this->count - $snip_count; |
199
|
|
|
|
200
|
|
|
$output = ($snip_fraction > 0) ? __( 'All snippets are ok', 'cgss' ) : sprintf(_n('%d page', '%d pages', $snip_fraction, 'cgss'), $snip_fraction) . ' ' . __( 'have incomplete snippets', 'cgss' ); |
|
|
|
|
201
|
|
|
return $output; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
//Fetch the scan results |
206
|
|
|
public function fetch() { |
207
|
|
|
|
208
|
|
|
global $wpdb; |
209
|
|
|
$sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type!='attachment'"; |
210
|
|
|
$ids = $wpdb->get_results( $sql, 'ARRAY_A' );; |
211
|
|
|
|
212
|
|
|
$data_piece = array(); |
213
|
|
|
foreach ($ids as $id) { |
214
|
|
|
|
215
|
|
|
$meta = get_post_meta( $id['ID'], 'cgss_scan_result', true ); |
|
|
|
|
216
|
|
|
if ($meta) { |
217
|
|
|
$data_piece[] = $meta; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$data = array(); |
222
|
|
|
$data['score'] = array_column($data_piece, 'score'); |
223
|
|
|
$data['snip'] = array_column($data_piece, 'snip'); |
224
|
|
|
$data['social'] = array_column($data_piece, 'social'); |
225
|
|
|
$data['text'] = array_column($data_piece, 'text'); |
226
|
|
|
$data['design'] = array_column($data_piece, 'design'); |
227
|
|
|
$data['crawl'] = array_column($data_piece, 'crawl'); |
228
|
|
|
$data['speed'] = array_column($data_piece, 'speed'); |
229
|
|
|
$data['social_tags'] = array_column($data_piece, 'social_tags'); |
230
|
|
|
|
231
|
|
|
return $data; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} ?> |
|
|
|
|