@@ -11,13 +11,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
1111 $ serverDirectory = realpath (__DIR__ . '/../.. ' );
1212 }
1313 if ( $ serverUrl === null ) {
14- //Default to the current URL minus the query and "index.php".
15- $ serverUrl = 'http:// ' . $ _SERVER ['HTTP_HOST ' ];
16- $ path = $ _SERVER ['SCRIPT_NAME ' ];
17- if ( basename ($ path ) === 'index.php ' ) {
18- $ path = dirname ($ path ) . '/ ' ;
19- }
20- $ serverUrl .= $ path ;
14+ $ serverUrl = self ::guessServerUrl ();
2115 }
2216
2317 $ this ->serverUrl = $ serverUrl ;
@@ -26,6 +20,56 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
2620 $ this ->cache = new Wpup_FileCache ($ serverDirectory . '/cache ' );
2721 }
2822
23+ /**
24+ * Guess the Server Url based on the current request.
25+ *
26+ * Defaults to the current URL minus the query and "index.php".
27+ *
28+ * @static
29+ *
30+ * @return string Url
31+ */
32+ public static function guessServerUrl () {
33+ $ serverUrl = ( self ::isSsl () ? 'https ' : 'http ' );
34+ $ serverUrl .= ':// ' . $ _SERVER ['HTTP_HOST ' ];
35+ $ path = $ _SERVER ['SCRIPT_NAME ' ];
36+
37+ if ( basename ($ path ) === 'index.php ' ) {
38+ $ dir = dirname ($ path );
39+ if ( DIRECTORY_SEPARATOR === '/ ' ) {
40+ $ path = $ dir . '/ ' ;
41+ } else {
42+ // Fix Windows
43+ $ path = str_replace ('\\' , '/ ' , $ dir );
44+ //Make sure there's a trailing slash.
45+ if ( substr ($ path , -1 ) !== '/ ' ) {
46+ $ path .= '/ ' ;
47+ }
48+ }
49+ }
50+
51+ $ serverUrl .= $ path ;
52+ return $ serverUrl ;
53+ }
54+
55+ /**
56+ * Determine if ssl is used.
57+ *
58+ * @see WP core - wp-includes/functions.php
59+ *
60+ * @return bool True if SSL, false if not used.
61+ */
62+ public static function isSsl () {
63+ if ( isset ($ _SERVER ['HTTPS ' ]) ) {
64+ if ( $ _SERVER ['HTTPS ' ] == '1 ' || strtolower ($ _SERVER ['HTTPS ' ]) === 'on ' ) {
65+ return true ;
66+ }
67+ } elseif ( isset ($ _SERVER ['SERVER_PORT ' ]) && ( '443 ' == $ _SERVER ['SERVER_PORT ' ] ) ) {
68+ return true ;
69+ }
70+ return false ;
71+ }
72+
2973 /**
3074 * Process an update API request.
3175 *
@@ -274,9 +318,13 @@ protected function filterLogInfo($columns) {
274318 */
275319 protected function outputAsJson ($ response ) {
276320 header ('Content-Type: application/json ' );
277- $ output = json_encode ($ response );
278- if ( function_exists ('wsh_pretty_json ' ) ) {
279- $ output = wsh_pretty_json ($ output );
321+ $ output = '' ;
322+ if ( defined ('JSON_PRETTY_PRINT ' ) ) {
323+ $ output = json_encode ($ response , JSON_PRETTY_PRINT );
324+ } elseif ( function_exists ('wsh_pretty_json ' ) ) {
325+ $ output = wsh_pretty_json (json_encode ($ response ));
326+ } else {
327+ $ output = json_encode ($ response );
280328 }
281329 echo $ output ;
282330 }
@@ -353,10 +401,13 @@ protected function exitWithError($message = '', $httpStatus = 500) {
353401 * You can also set an argument to NULL to remove it.
354402 *
355403 * @param array $args An associative array of query arguments.
356- * @param string $url The old URL.
404+ * @param string $url The old URL. Optional, defaults to the request url without query arguments.
357405 * @return string New URL.
358406 */
359- protected static function addQueryArg ($ args , $ url ) {
407+ protected static function addQueryArg ($ args , $ url = null ) {
408+ if ( !isset ($ url ) ) {
409+ $ url = self ::guessServerUrl ();
410+ }
360411 if ( strpos ($ url , '? ' ) !== false ) {
361412 $ parts = explode ('? ' , $ url , 2 );
362413 $ base = $ parts [0 ] . '? ' ;
0 commit comments