Passed
Push — master ( ab9b1d...ea9273 )
by Nirjhar
04:28
created

CGSS_INSIGHT::snippet_analyze()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 15
rs 9.6111
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();
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $update is dead and can be removed.
Loading history...
39
			}
40
		}
41
42
43
		//Compile the result
44
		public function compile() {
45
46
			$score = $this->data['score'];
47
			$this->count = count($score);
0 ignored issues
show
Bug Best Practice introduced by
The property count does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
			$avarage_score = round(array_sum($score)/count($score), 0);
49
50
			$this->score = sprintf(__('Avarage SEO score is %d out of 10', 'cgss'),$avarage_score);
0 ignored issues
show
Bug Best Practice introduced by
The property score does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
			$this->score = sprintf(/** @scrutinizer ignore-call */ __('Avarage SEO score is %d out of 10', 'cgss'),$avarage_score);
Loading history...
51
			$this->snippet = $this->snippet_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property snippet does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
			$this->text = $this->text_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
			$this->links = $this->link_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property links does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
			$this->keywords = $this->keyword_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property keywords does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
			$this->images = $this->image_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property images does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
			$this->responsive = $this->responsivity();
0 ignored issues
show
Bug Best Practice introduced by
The property responsive does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
			$this->speed = $this->speed_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property speed does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
			$this->social = $this->social_analyze();
0 ignored issues
show
Bug Best Practice introduced by
The property social does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
		}
60
61
62
		//Analyze snippets
63
		public function snippet_analyze() {
64
65
			$snippets = $this->data['snip'];
66
			$snip_count = 0;
67
			foreach ($snippets as $snippet) {
68
				$title = $snippet['title'];
69
				$desc = $snippet['desc'];
70
				if (!empty($title) && !empty($desc)) {
71
					$snip_count++;
72
				}
73
			}
74
			$snip_fraction = $this->count - $snip_count;
75
76
			$output = ($snip_fraction == 0) ? __( 'All snippets are ok', 'cgss' ) : sprintf(_n('%d page', '%d pages', $snip_fraction, 'cgss'), $snip_fraction) . ' ' . __( 'have incomplete snippets', 'cgss' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
			$output = ($snip_fraction == 0) ? /** @scrutinizer ignore-call */ __( 'All snippets are ok', 'cgss' ) : sprintf(_n('%d page', '%d pages', $snip_fraction, 'cgss'), $snip_fraction) . ' ' . __( 'have incomplete snippets', 'cgss' );
Loading history...
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
			$output = ($snip_fraction == 0) ? __( 'All snippets are ok', 'cgss' ) : sprintf(/** @scrutinizer ignore-call */ _n('%d page', '%d pages', $snip_fraction, 'cgss'), $snip_fraction) . ' ' . __( 'have incomplete snippets', 'cgss' );
Loading history...
77
			return $output;
78
		}
79
80
81
		//Analyze text
82
		public function text_analyze() {
83
84
			$text = $this->data['text'];
85
			$count = round(array_sum(array_column($text, 'count')) / $this->count, 0);
86
			$ratio = round(array_sum(array_column($text, 'ratio')) / $this->count, 2);
87
88
			$output = sprintf(__('Avarage', 'cgss') . ' ' . _n('%d word is','%d words are',$count,'cgss') . ' ' . __( 'found per page and avarage text to HTML ratio is %d', 'cgss'),$count,$ratio) . '%';
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage', 'cgss') . ' ' . _n('%d word is','%d words are',$count,'cgss') . ' ' . __( 'found per page and avarage text to HTML ratio is %d', 'cgss'),$count,$ratio) . '%';
Loading history...
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
			$output = sprintf(__('Avarage', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n('%d word is','%d words are',$count,'cgss') . ' ' . __( 'found per page and avarage text to HTML ratio is %d', 'cgss'),$count,$ratio) . '%';
Loading history...
89
			return $output;
90
		}
91
92
93
		// Analysis of links
94
		public function link_analyze() {
95
96
			$text = $this->data['text'];
97
			$links = array_column($text, 'links');
98
			$count = round(array_sum(array_column($links, 'count')) / $this->count, 0);
99
			$external = round(array_sum(array_column($links, 'external')) / $this->count, 0);
100
			$nofollow = round(array_sum(array_column($links, 'external')) / $this->count, 0);
101
			$external_percentage = round(($external/$count)*100, 0);
102
			$nofollow_percentage = round(($nofollow/$count)*100, 0);
103
104
			$output = sprintf(__('Avarage', 'cgss') . ' '.  _n( '%d link is','%d links are', $count, 'cgss' ) . ' ' . __('found per page. %d&#37; are external and %d&#37; are nofollow among them.', 'cgss'),$count,$external_percentage,$nofollow_percentage);
0 ignored issues
show
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
			$output = sprintf(__('Avarage', 'cgss') . ' '.  /** @scrutinizer ignore-call */ _n( '%d link is','%d links are', $count, 'cgss' ) . ' ' . __('found per page. %d&#37; are external and %d&#37; are nofollow among them.', 'cgss'),$count,$external_percentage,$nofollow_percentage);
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage', 'cgss') . ' '.  _n( '%d link is','%d links are', $count, 'cgss' ) . ' ' . __('found per page. %d&#37; are external and %d&#37; are nofollow among them.', 'cgss'),$count,$external_percentage,$nofollow_percentage);
Loading history...
105
			return $output;
106
		}
107
108
109
		// Analyze keywords
110
		public function keyword_analyze() {
111
112
			$text = $this->data['text'];
113
			$keywords = array_column($text, 'keys');
114
115
			$key_collect = array();
116
			$percent_collect = array();
117
			foreach ($keywords as $keyword) {
118
				$keys = array_keys($keyword);
119
				$top_key = $keys[0];
120
				$key_collect[] = count(explode(' ', $top_key));
121
				$percent_collect[] = $keyword[$top_key];
122
			}
123
124
			$key_count = round(array_sum($key_collect) / $this->count, 1);
125
			$percent = round(array_sum($percent_collect) / $this->count, 1);
126
127
			$output = sprintf(__('Avarage foucs keyword is', 'cgss') . ' ' . _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long. Keyword frequency of %d&#37;','cgss'),$key_count,$percent);
0 ignored issues
show
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
			$output = sprintf(__('Avarage foucs keyword is', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long. Keyword frequency of %d&#37;','cgss'),$key_count,$percent);
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage foucs keyword is', 'cgss') . ' ' . _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long. Keyword frequency of %d&#37;','cgss'),$key_count,$percent);
Loading history...
128
129
			return $output;
130
		}
131
132
133
		// Analyze images
134
		public function image_analyze() {
135
136
			$design = $this->data['design'];
137
			$images = array_column($design, 'image');
138
139
			$image_count = array_sum(array_column($images, 'count'));
140
			$no_alt_image = array_sum(array_column($images, 'no_alt_count'));
141
142
			$avg_image = round(($image_count/$this->count), 0);
143
144
			$output = sprintf(__('Avarage', 'cgss') . ' ' . _n( '%d image is', '%d images are', $avg_image, 'cgss' ) . ' ' . __( 'found per page.', 'cgss'),$avg_image) . ' ';
0 ignored issues
show
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
			$output = sprintf(__('Avarage', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n( '%d image is', '%d images are', $avg_image, 'cgss' ) . ' ' . __( 'found per page.', 'cgss'),$avg_image) . ' ';
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage', 'cgss') . ' ' . _n( '%d image is', '%d images are', $avg_image, 'cgss' ) . ' ' . __( 'found per page.', 'cgss'),$avg_image) . ' ';
Loading history...
145
			if ($no_alt_image == 0) {
146
				$output .= ' ' . __('All of them are optimized', 'cgss');
147
			} else {
148
				$no_alt_percent = round(($no_alt_image/$image_count)*100, 0);
149
				$output .= ' ' . sprintf(__('%d&#37; among them doesn\'t have alt tag', 'cgss'),$no_alt_percent);
150
			}
151
			return $output;
152
153
		}
154
155
156
		// Check mobile optimized
157
		public function responsivity() {
158
159
			$design = $this->data['design'];
160
			$vport = array_sum(array_column($design, 'vport'));
161
162
			if ($vport == $this->count) {
163
				$output = __('All pages are mobile optimized', 'cgss');
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
				$output = /** @scrutinizer ignore-call */ __('All pages are mobile optimized', 'cgss');
Loading history...
164
			} else {
165
				$no_mobile_percent = round(($vport/$this->count)*100, 0);
166
				$output = sprintf(__('%d&#37; pages aren\'t mobile optimized', 'cgss'),$no_mobile_percent);
167
			}
168
169
			return $output;
170
		}
171
172
173
		// Speed analyze
174
		public function speed_analyze() {
175
176
			$speed = $this->data['speed'];
177
178
			$res_time = array_sum(array_column($speed, 'res_time'));
179
			$down_time = array_sum(array_column($speed, 'down_time'));
180
181
			$avg_res_time = round(($res_time/$this->count) * 1000, 0);
182
			$avg_down_time = round(($down_time/$this->count) * 1000, 0);
183
184
			$output = sprintf( __('Average response time is %d miliseconds and average download time is %d miliseconds', 'cgss'), $avg_res_time, $avg_down_time);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
			$output = sprintf( /** @scrutinizer ignore-call */ __('Average response time is %d miliseconds and average download time is %d miliseconds', 'cgss'), $avg_res_time, $avg_down_time);
Loading history...
185
186
			return $output;
187
		}
188
189
190
		// Count social shares
191
		public function social_analyze() {
192
193
			$social = $this->data['social'];
194
195
			$gplus = array_sum(array_column($social, 'gplus'));
196
			$fb_share = array_sum(array_column($social, 'fb_share'));
197
			$fb_like = array_sum(array_column($social, 'fb_like'));
198
199
			$output = sprintf( __('Total', 'cgss') . ': ' . _n( '%d share', '%d shares', $gplus, 'cgss' ) . ' ' . __( 'in g+', 'cgss' ). ' | ' . _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $gplus, $fb_share, $fb_like);
0 ignored issues
show
Bug introduced by
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
			$output = sprintf( __('Total', 'cgss') . ': ' . /** @scrutinizer ignore-call */ _n( '%d share', '%d shares', $gplus, 'cgss' ) . ' ' . __( 'in g+', 'cgss' ). ' | ' . _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $gplus, $fb_share, $fb_like);
Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
			$output = sprintf( /** @scrutinizer ignore-call */ __('Total', 'cgss') . ': ' . _n( '%d share', '%d shares', $gplus, 'cgss' ) . ' ' . __( 'in g+', 'cgss' ). ' | ' . _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $gplus, $fb_share, $fb_like);
Loading history...
200
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 );
0 ignored issues
show
Bug introduced by
The function get_post_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
				$meta = /** @scrutinizer ignore-call */ get_post_meta( $id['ID'], 'cgss_scan_result', true );
Loading history...
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
} ?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
235