ConnectionTest::check_internet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
namespace testContent;
3
4
/**
5
 * ConnectionTest
6
 *
7
 * A more intelligent check to see if we can connect to Splashbase or not.
8
 *
9
 * This class checks whether or not we can connect to the Internet, and
10
 * if we can, whether we can connect to Splashbase itself. This is used by
11
 * our admin notice function to check whether or not we should display a notice
12
 * to users warning them of issues with Splashbase.
13
 *
14
 * The purpose of this is to avoid useless bug-hunting when images don't work.
15
 *
16
 * @package    WordPress
17
 * @subpackage Evans
18
 * @author     Old Town Media
19
 */
20
class ConnectionTest{
21
22
	/**
23
	 * Run all of our connection tests.
24
	 *
25
	 * @see check_admin_page, check_airplane_mode, check_internet, check_splashbase
26
	 *
27
	 * @return boolean Status of connection to Internet/Splashbase.
28
	 */
29
	public function test(){
30
31
		/*
32
		 * Make sure that we're looking at the correct admin page
33
		 */
34
		if ( ! $this->check_admin_page() ){
35
			return;
36
		}
37
38
		/*
39
		 * Test #1 - Check for Airplane Mode plugin status
40
		 */
41
		if ( ! $this->check_airplane_mode() ){
42
			return false;
43
		}
44
45
		/*
46
		 * Test #2 - Check Internet connection in general
47
		 */
48
		if ( ! $this->check_internet() ){
49
			return false;
50
		}
51
52
		/*
53
		 * Test #3 - Check External URL itself (Splashbase here)
54
		 */
55
		if ( ! $this->check_external_url( 'http://www.splashbase.co/api/v1/images/' ) ){
56
			return false;
57
		}
58
59
		// We've made it this far, looks like everything checks out OK!
60
		return true;
61
62
	}
63
64
65
	/**
66
	 * Check to make sure that we're only running this check in the correct place.
67
	 *
68
	 * We only want these (relatively)expensive checks to run on a single admin
69
	 * page, so we need to run some checks first and verify that we're on the
70
	 * right screen.
71
	 *
72
	 * @access private
73
	 *
74
	 * @global object $current_screen Current admin screen info.
75
	 *
76
	 * @return boolean Whether or not we're in the right place.
77
	 */
78
	private function check_admin_page(){
79
		global $current_screen;
80
81
		// Only run if we're in the admin page
82
		if ( !is_admin() ){
83
			return false;
84
		}
85
86
		// Get the current admin screen & verify that we're on the right one
87
		// before continuing.
88
		if ( isset ( $current_screen ) && 'tools_page_create-test-data' != $current_screen->base ){
89
			return false;
90
		}
91
92
		$last_uri_bit = explode( '=', $_SERVER['REQUEST_URI'] );
93
		if ( 'create-test-data' != end( $last_uri_bit ) ){
94
			return false;
95
		}
96
97
		return true;
98
99
	}
100
101
102
	/**
103
	 * Check if we have Airplane Mode and if it's turned on or not.
104
	 *
105
	 * @access private
106
	 *
107
	 * @see get_site_option
108
	 *
109
	 * @return boolean Connected or not.
110
	 */
111
	private function check_airplane_mode(){
112
113
		if ( class_exists( 'Airplane_Mode_Core' ) ){
114
			// Is airplane mode active?
115
			$airplane_mode = get_site_option( 'airplane-mode' );
116
117
			if ( $airplane_mode === 'on' ){
118
				return false;
119
			}
120
		}
121
122
		return true;
123
124
	}
125
126
127
	/**
128
	 * Attempt to open a socket to a popular, compact to check overall connectivity.
129
	 *
130
	 * @access private
131
	 *
132
	 * @see fsockopen, fsockclose
133
	 *
134
	 * @return boolean Connected or not.
135
	 */
136
	private function check_internet(){
137
138
		// Attempt to open a socket connection to Google
139
		$connected = @fsockopen( "www.google.com", 80 );
140
141
		if ( !$connected ){
142
			return false;
143
		}
144
145
		// Close out our 1st test
146
		fclose( $connected );
147
148
		return true;
149
150
	}
151
152
153
	/**
154
	 * Check an external API to see if it's reachable or not.
155
	 *
156
	 * @access private
157
	 *
158
	 * @see wp_remote_get
159
	 *
160
	 * @param string $url External URL to attempt to reach.
161
	 * @return boolean Connected or not.
162
	 */
163
	private function check_external_url( $url ){
164
165
		$test_url = esc_url( $url );
166
		$response = wp_remote_get( $test_url );
167
168
		if ( !is_array( $response ) ){
169
			return false;
170
		}
171
172
		return true;
173
174
	}
175
176
}
177