PluginProbe
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs / 5.0.1
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs v5.0.1
5.0.1 4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 All 39 releases
← All changes | freemius/includes/sdk/FreemiusWordPress.php +65 -32 1.3.4 → 5.0.1 View file →
@@ -13,8 +13,11 @@
13 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 14 * License for the specific language governing permissions and limitations
15 15 * under the License.
16 16 */
17 + if ( ! defined( 'ABSPATH' ) ) {
18 + exit;
19 + }
17 20
18 21 require_once dirname( __FILE__ ) . '/FreemiusBase.php';
19 22
20 23 if ( ! defined( 'FS_SDK__USER_AGENT' ) ) {
@@ -81,12 +84,9 @@
81 84 if ( ! defined( 'FS_API__SANDBOX_ADDRESS' ) ) {
82 85 define( 'FS_API__SANDBOX_ADDRESS', '://sandbox-api.freemius.com' );
83 86 }
84 87
85 - if ( class_exists( 'Freemius_Api_WordPress' ) ) {
86 - return;
87 - }
88 -
88 + if ( ! class_exists( 'Freemius_Api_WordPress' ) ) {
89 89 class Freemius_Api_WordPress extends Freemius_Api_Base {
90 90 private static $_logger = array();
91 91
92 92 /**
@@ -163,8 +163,17 @@
163 163 public static function SetHttp() {
164 164 self::$_protocol = 'http';
165 165 }
166 166
167 + /**
168 + * Sets API connection protocol to HTTPS.
169 + *
170 + * @since 2.5.4
171 + */
172 + public static function SetHttps() {
173 + self::$_protocol = 'https';
174 + }
175 +
167 176 /**
168 177 * @since 1.0.4
169 178 *
170 179 * @return bool
@@ -301,11 +310,13 @@
301 310 *
302 311 * @return mixed
303 312 */
304 313 private static function ExecuteRequest( $pUrl, &$pWPRemoteArgs ) {
314 + $bt = debug_backtrace();
315 +
305 316 $start = microtime( true );
306 317
307 - $response = wp_remote_request( $pUrl, $pWPRemoteArgs );
318 + $response = self::RemoteRequest( $pUrl, $pWPRemoteArgs );
308 319
309 320 if ( FS_API__LOGGER_ON ) {
310 321 $end = microtime( true );
311 322
@@ -323,9 +334,9 @@
323 334 'result' => ! $is_http_error ?
324 335 $response['body'] :
325 336 json_encode( $response->get_error_messages() ),
326 337 'code' => ! $is_http_error ? $response['response']['code'] : null,
327 - 'backtrace' => debug_backtrace(),
338 + 'backtrace' => $bt,
328 339 );
329 340 }
330 341
331 342 return $response;
@@ -330,8 +341,33 @@
330 341
331 342 return $response;
332 343 }
333 344
345 + /**
346 + * @author Leo Fajardo (@leorw)
347 + *
348 + * @param string $pUrl
349 + * @param array $pWPRemoteArgs
350 + *
351 + * @return array|WP_Error The response array or a WP_Error on failure.
352 + */
353 + static function RemoteRequest( $pUrl, $pWPRemoteArgs ) {
354 + $response = wp_remote_request( $pUrl, $pWPRemoteArgs );
355 +
356 + if (
357 + is_array( $response ) &&
358 + (
359 + empty( $response['headers'] ) ||
360 + empty( $response['headers']['x-api-server'] )
361 + )
362 + ) {
363 + // API is considered blocked if the response doesn't include the `x-api-server` header. When there's no error but this header doesn't exist, the response is usually not in the expected form (e.g., cannot be JSON-decoded).
364 + $response = new WP_Error( 'api_blocked', htmlentities( $response['body'] ) );
365 + }
366 +
367 + return $response;
368 + }
369 +
334 370 /**
335 371 * @return array
336 372 */
337 373 static function GetLogger() {
@@ -371,9 +407,9 @@
371 407
372 408 $pWPRemoteArgs = array(
373 409 'method' => strtoupper( $pMethod ),
374 410 'connect_timeout' => 10,
375 - 'timeout' => 60,
411 + 'timeout' => WP_FS__API_TIMEOUT,
376 412 'follow_redirects' => true,
377 413 'redirection' => 5,
378 414 'user-agent' => $user_agent,
379 415 'blocking' => true,
@@ -436,10 +472,14 @@
436 472 * @author Vova Feldman (@svovaf)
437 473 */
438 474 if ( filter_var( $matches[1], FILTER_VALIDATE_IP ) ) {
439 475 if ( strlen( inet_pton( $matches[1] ) ) === 16 ) {
440 -// error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.');
441 - // Hook to an action triggered just before cURL is executed to resolve the IP version to v4.
476 + /**
477 + * error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.');
478 + * Hook to an action triggered just before cURL is executed to resolve the IP version to v4.
479 + *
480 + * @phpstan-ignore-next-line
481 + */
442 482 add_action( 'http_api_curl', 'Freemius_Api_WordPress::CurlResolveToIPv4', 10, 1 );
443 483
444 484 // Re-run request.
445 485 $result = self::ExecuteRequest( $request_url, $pWPRemoteArgs );
@@ -540,31 +580,23 @@
540 580 #----------------------------------------------------------------------------------
541 581 #region Connectivity Test
542 582 #----------------------------------------------------------------------------------
543 583
544 - /**
545 - * If successful connectivity to the API endpoint using ping.json endpoint.
546 - *
547 - * - OR -
548 - *
549 - * Validate if ping result object is valid.
550 - *
551 - * @param mixed $pPong
552 - *
553 - * @return bool
554 - */
555 - public static function Test( $pPong = null ) {
556 - $pong = is_null( $pPong ) ?
557 - self::Ping() :
558 - $pPong;
584 + /**
585 + * This method exists only for backward compatibility to prevent a fatal error from happening when called from an outdated piece of code.
586 + *
587 + * @param mixed $pPong
588 + *
589 + * @return bool
590 + */
591 + public static function Test( $pPong = null ) {
592 + return (
593 + is_object( $pPong ) &&
594 + isset( $pPong->api ) &&
595 + 'pong' === $pPong->api
596 + );
597 + }
559 598
560 - return (
561 - is_object( $pong ) &&
562 - isset( $pong->api ) &&
563 - 'pong' === $pong->api
564 - );
565 - }
566 -
567 599 /**
568 600 * Ping API to test connectivity.
569 601 *
570 602 * @return object
@@ -708,5 +740,6 @@
708 740 ) );
709 741 }
710 742
711 743 #endregion
712 - }
744 + }
745 + }