Issues (203)

Plugin/Lib/Action/insight.php (31 issues)

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
			try{
16
				$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...
17
				$this->compile();
18
				$this->save();
19
			} catch(Exception $e) {
0 ignored issues
show
The type NirjharLo\Cgss\Lib\Action\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
20
				echo __( 'Something went wrong', 'cgss') . ' ' . $e->get_message();
0 ignored issues
show
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

20
				echo /** @scrutinizer ignore-call */ __( 'Something went wrong', 'cgss') . ' ' . $e->get_message();
Loading history...
21
			}
22
		}
23
24
25
		//DB insert function
26
		public function save() {
27
28
			global $wpdb;
29
30
			$result = array(
31
				$this->score,
32
				$this->snippet,
33
				$this->text,
34
				$this->links,
35
				$this->keywords,
36
				$this->images,
37
				$this->responsive,
38
				$this->speed,
39
				$this->social,
40
			);
41
			foreach ($result as $key => $value) {
42
				$sql = $wpdb->prepare("UPDATE {$wpdb->prefix}cgss_insight SET remark = %s WHERE ID = %d", $value, ($key+1));
43
				$update = $wpdb->query($sql);
0 ignored issues
show
The assignment to $update is dead and can be removed.
Loading history...
44
			}
45
		}
46
47
48
		//Compile the result
49
		public function compile() {
50
51
			$score = $this->data['score'];
52
			$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...
53
			$avarage_score = (count($score) == 0 ? 0 : round(array_sum($score)/count($score), 0));
54
55
			$this->score = sprintf(__('Avarage SEO score is %d out of 10', 'cgss'),$avarage_score);
0 ignored issues
show
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

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

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

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

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

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

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

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

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

132
			$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...
133
134
			return $output;
135
		}
136
137
138
		// Analyze images
139
		public function image_analyze() {
140
141
			$design = $this->data['design'];
142
			$images = array_column($design, 'image');
143
144
			$image_count = array_sum(array_column($images, 'count'));
145
			$no_alt_image = array_sum(array_column($images, 'no_alt_count'));
146
147
			$avg_image = round(($image_count/$this->count), 0);
148
149
			$output = sprintf(__('Avarage', 'cgss') . ' ' . _n( '%d image is', '%d images are', $avg_image, 'cgss' ) . ' ' . __( 'found per page.', 'cgss'),$avg_image) . ' ';
0 ignored issues
show
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

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

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

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

189
			$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...
190
191
			return $output;
192
		}
193
194
195
		// Count social shares
196
		public function social_analyze() {
197
198
			$social = $this->data['social'];
199
200
			$fb_share = array_sum(array_column($social, 'fb_share'));
201
			$fb_like = array_sum(array_column($social, 'fb_like'));
202
203
			$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
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

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

203
			$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...
204
205
			return $output;
206
		}
207
208
209
		//Fetch the scan results
210
		public function fetch() {
211
212
			global $wpdb;
213
			$sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type!='attachment'";
214
			$ids = $wpdb->get_results( $sql, 'ARRAY_A' );;
215
216
			$data_piece = array();
217
			foreach ($ids as $id) {
218
219
				$meta = get_post_meta( $id['ID'], 'cgss_scan_result', true );
0 ignored issues
show
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

219
				$meta = /** @scrutinizer ignore-call */ get_post_meta( $id['ID'], 'cgss_scan_result', true );
Loading history...
220
				if ($meta) {
221
					$data_piece[] = $meta;
222
				}
223
			}
224
225
			$data = array();
226
			$data['score'] = array_column($data_piece, 'score');
227
			$data['snip'] = array_column($data_piece, 'snip');
228
			$data['social'] = array_column($data_piece, 'social');
229
			$data['text'] = array_column($data_piece, 'text');
230
			$data['design'] = array_column($data_piece, 'design');
231
			$data['crawl'] = array_column($data_piece, 'crawl');
232
			$data['speed'] = array_column($data_piece, 'speed');
233
			$data['social_tags'] = array_column($data_piece, 'social_tags');
234
235
			return $data;
236
		}
237
	} ?>
0 ignored issues
show
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...
238