Passed
Pull Request — master (#2)
by Nirjhar
05:45
created

Insight   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 118
dl 0
loc 222
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A image_analyze() 0 18 2
A snippet_analyze() 0 15 5
A save() 0 18 2
A link_analyze() 0 12 3
A compile() 0 15 3
A social_analyze() 0 10 1
A fetch() 0 26 3
A responsivity() 0 13 2
A keyword_analyze() 0 20 2
A __construct() 0 5 1
A speed_analyze() 0 13 1
A text_analyze() 0 8 1
1
<?php
2
namespace NirjharLo\Cgss\Lib\Action;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
7
/**
8
 * Perform fetch insight action
9
 */
10
	final class Insight {
11
12
13
		public function __construct() {
14
15
				$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...
16
				$this->compile();
17
				$this->save();
18
		}
19
20
21
		//DB insert function
22
		public function save() {
23
24
			global $wpdb;
25
26
			$result = array(
27
				$this->score,
28
				$this->snippet,
29
				$this->text,
30
				$this->links,
31
				$this->keywords,
32
				$this->images,
33
				$this->responsive,
34
				$this->speed,
35
				$this->social,
36
			);
37
			foreach ($result as $key => $value) {
38
				$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}cgss_insight SET remark = %s WHERE ID = %d", $value, ($key+1));
39
				$update = $wpdb->query($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $update is dead and can be removed.
Loading history...
40
			}
41
		}
42
43
44
		//Compile the result
45
		public function compile() {
46
47
			$score = $this->data['score'];
48
			$this->count = (count($score) == 0 ? 1 : 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...
49
			$avarage_score = (count($score) == 0 ? 0 : round(array_sum($score)/count($score), 0));
50
51
			$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

51
			$this->score = sprintf(/** @scrutinizer ignore-call */ __('Avarage SEO score is %d out of 10', 'cgss'),$avarage_score);
Loading history...
52
			$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...
53
			$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...
54
			$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...
55
			$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...
56
			$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...
57
			$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...
58
			$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...
59
			$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...
60
		}
61
62
63
		//Analyze snippets
64
		public function snippet_analyze() {
65
66
			$snippets = $this->data['snip'];
67
			$snip_count = 0;
68
			foreach ($snippets as $snippet) {
69
				$title = $snippet['title'];
70
				$desc = $snippet['desc'];
71
				if (!empty($title) && !empty($desc)) {
72
					$snip_count++;
73
				}
74
			}
75
			$snip_fraction = $this->count - $snip_count;
76
77
			$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

77
			$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

77
			$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...
78
			return $output;
79
		}
80
81
82
		//Analyze text
83
		public function text_analyze() {
84
85
			$text = $this->data['text'];
86
			$count = round(array_sum(array_column($text, 'count')) / $this->count, 0);
87
			$ratio = round(array_sum(array_column($text, 'ratio')) / $this->count, 2);
88
89
			$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 _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

89
			$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...
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

89
			$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...
90
			return $output;
91
		}
92
93
94
		// Analysis of links
95
		public function link_analyze() {
96
97
			$text = $this->data['text'];
98
			$links = array_column($text, 'links');
99
			$count = round(array_sum(array_column($links, 'count')) / $this->count, 0);
100
			$external = round(array_sum(array_column($links, 'external')) / $this->count, 0);
101
			$nofollow = round(array_sum(array_column($links, 'external')) / $this->count, 0);
102
			$external_percentage = ($count == 0 ? 0 : round(($external/$count)*100, 0));
103
			$nofollow_percentage = ($count == 0 ? 0 : round(($nofollow/$count)*100, 0));
104
105
			$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 __ 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

105
			$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...
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

105
			$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...
106
			return $output;
107
		}
108
109
110
		// Analyze keywords
111
		public function keyword_analyze() {
112
113
			$text = $this->data['text'];
114
			$keywords = array_column($text, 'keys');
115
116
			$key_collect = array();
117
			$percent_collect = array();
118
			foreach ($keywords as $keyword) {
119
				$keys = array_keys($keyword);
120
				$top_key = $keys[0];
121
				$key_collect[] = count(explode(' ', $top_key));
122
				$percent_collect[] = $keyword[$top_key];
123
			}
124
125
			$key_count = round(array_sum($key_collect) / $this->count, 1);
126
			$percent = round(array_sum($percent_collect) / $this->count, 1);
127
128
			$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 __ 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

128
			$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...
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

128
			$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...
129
130
			return $output;
131
		}
132
133
134
		// Analyze images
135
		public function image_analyze() {
136
137
			$design = $this->data['design'];
138
			$images = array_column($design, 'image');
139
140
			$image_count = array_sum(array_column($images, 'count'));
141
			$no_alt_image = array_sum(array_column($images, 'no_alt_count'));
142
143
			$avg_image = round(($image_count/$this->count), 0);
144
145
			$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 __ 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

145
			$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...
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

145
			$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...
146
			if ($no_alt_image == 0) {
147
				$output .= ' ' . __('All of them are optimized', 'cgss');
148
			} else {
149
				$no_alt_percent = round(($no_alt_image/$image_count)*100, 0);
150
				$output .= ' ' . sprintf(__('%d&#37; among them doesn\'t have alt tag', 'cgss'),$no_alt_percent);
151
			}
152
			return $output;
153
154
		}
155
156
157
		// Check mobile optimized
158
		public function responsivity() {
159
160
			$design = $this->data['design'];
161
			$vport = array_sum(array_column($design, 'vport'));
162
163
			if ($vport == $this->count) {
164
				$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

164
				$output = /** @scrutinizer ignore-call */ __('All pages are mobile optimized', 'cgss');
Loading history...
165
			} else {
166
				$no_mobile_percent = round(($vport/$this->count)*100, 0);
167
				$output = sprintf(__('%d&#37; pages aren\'t mobile optimized', 'cgss'),$no_mobile_percent);
168
			}
169
170
			return $output;
171
		}
172
173
174
		// Speed analyze
175
		public function speed_analyze() {
176
177
			$speed = $this->data['speed'];
178
179
			$res_time = array_sum(array_column($speed, 'res_time'));
180
			$down_time = array_sum(array_column($speed, 'down_time'));
181
182
			$avg_res_time = round(($res_time/$this->count) * 1000, 0);
183
			$avg_down_time = round(($down_time/$this->count) * 1000, 0);
184
185
			$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

185
			$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...
186
187
			return $output;
188
		}
189
190
191
		// Count social shares
192
		public function social_analyze() {
193
194
			$social = $this->data['social'];
195
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', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $fb_share, $fb_like);
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

199
			$output = sprintf( /** @scrutinizer ignore-call */ __('Total', 'cgss') . ': ' . _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $fb_share, $fb_like);
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

199
			$output = sprintf( __('Total', 'cgss') . ': ' . /** @scrutinizer ignore-call */ _n( '%d share', '%d shares', $fb_share, 'cgss' ) . ' ' . __( 'in Facebook', 'cgss' ). ' | ' . _n( '%d Facebook like', '%d Facebook likes', $fb_like, 'cgss' ), $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
	} ?>
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...
234