Passed
Push — master ( e0c498...d80a24 )
by Nirjhar
04:11
created

CGSS_INSIGHT::social_analyze()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
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', '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', '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->resposivity();
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
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);
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

74
			$output = sprintf(/** @scrutinizer ignore-call */ __('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);
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

74
			$output = sprintf(__('Avarage', 'cgss') . ' '.  /** @scrutinizer ignore-call */ _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);
Loading history...
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);
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

97
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage foucs keyword is', 'cgss') . ' ' . _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long with keywords frequency of %d percent','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

97
			$output = sprintf(__('Avarage foucs keyword is', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n( '%d word','%d words',$key_count, 'cgss' ) . ' ' . __('long with keywords frequency of %d percent','cgss'),$key_count,$percent);
Loading history...
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) . ' ';
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

114
			$output = sprintf(__('Avarage', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n( '%d image', '%d images', $avg_image, 'cgss' ) . ' ' . __( 'are found per page and', '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

114
			$output = sprintf(/** @scrutinizer ignore-call */ __('Avarage', 'cgss') . ' ' . _n( '%d image', '%d images', $avg_image, 'cgss' ) . ' ' . __( 'are found per page and', 'cgss'),$avg_image) . ' ';
Loading history...
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');
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

133
				$output = /** @scrutinizer ignore-call */ __('All pages are mobile optimized', 'cgss');
Loading history...
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);
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

153
			$output = sprintf( /** @scrutinizer ignore-call */ __('Average response time is %d s and average download time is %d s', 'cgss'), $avg_res_time, $avg_down_time);
Loading history...
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);
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

168
			$output = sprintf( /** @scrutinizer ignore-call */ __('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);
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

168
			$output = sprintf( __('Total', 'cgss' ). ' ' . /** @scrutinizer ignore-call */ _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);
Loading history...
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);
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

181
			$output = sprintf(/** @scrutinizer ignore-call */ __('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);
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

181
			$output = sprintf(__('Avarage', 'cgss') . ' ' . /** @scrutinizer ignore-call */ _n('%d word','%d words',$count,'cgss') . ' ' . __( 'are found per page and avarage text to HTML ratio is %d percent', 'cgss'),$count,$ratio);
Loading history...
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' );
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

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

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