Issues (206)

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

1
<?php
2
namespace NirjharLo\Cgss\Lib\Action;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
use \NirjharLo\Cgss\Models\Insight as InsightModel;
0 ignored issues
show
The type \NirjharLo\Cgss\Models\Insight was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
9
/**
10
 * Perform fetch insight action
11
 */
12
	final class Insight {
13
14
15
		public function __construct() {
16
17
			try{
18
				$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...
19
				$this->compile();
20
				$this->save();
21
			} 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...
22
				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

22
				echo /** @scrutinizer ignore-call */ __( 'Something went wrong', 'cgss') . ' ' . $e->get_message();
Loading history...
23
			}
24
		}
25
26
27
		//DB insert function
28
                public function save() {
29
30
                        $model = new InsightModel();
31
32
                        $result = array(
33
                                $this->score,
34
                                $this->snippet,
35
                                $this->text,
36
                                $this->links,
37
                                $this->keywords,
38
                                $this->images,
39
                                $this->responsive,
40
                                $this->speed,
41
                                $this->social,
42
                        );
43
                        foreach ($result as $key => $value) {
44
                                $model->updateRemark($key + 1, $value);
45
                        }
46
                }
47
48
49
		//Compile the result
50
		public function compile() {
51
52
			$score = $this->data['score'];
53
			$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...
54
			$avarage_score = (count($score) == 0 ? 0 : round(array_sum($score)/count($score), 0));
55
56
			$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...
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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