: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Copyright 2014 Freemius, Inc.
* Licensed under the GPL v2 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
* http://choosealicense.com/licenses/gpl-v2/
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
if ( ! defined( 'ABSPATH' ) ) {
if ( ! defined( 'FS_API__VERSION' ) ) {
define( 'FS_API__VERSION', '1' );
if ( ! defined( 'FS_SDK__PATH' ) ) {
define( 'FS_SDK__PATH', dirname( __FILE__ ) );
if ( ! defined( 'FS_SDK__EXCEPTIONS_PATH' ) ) {
define( 'FS_SDK__EXCEPTIONS_PATH', FS_SDK__PATH . '/Exceptions/' );
if ( ! function_exists( 'json_decode' ) ) {
throw new Exception( 'Freemius needs the JSON PHP extension.' );
// Include all exception files.
'InvalidArgumentException',
'ArgumentNotExistException',
'EmptyArgumentException',
foreach ( $exceptions as $e ) {
require_once FS_SDK__EXCEPTIONS_PATH . $e . '.php';
if ( ! class_exists( 'Freemius_Api_Base' ) ) {
abstract class Freemius_Api_Base {
* @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'.
* @param number $pID Element's id.
* @param string $pPublic Public key.
* @param string $pSecret Element's secret key.
* @param bool $pIsSandbox Whether or not to run API in sandbox mode.
public function Init( $pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false ) {
$this->_public = $pPublic;
$this->_secret = $pSecret;
$this->_isSandbox = $pIsSandbox;
public function IsSandbox() {
return $this->_isSandbox;
function CanonizePath( $pPath ) {
$pPath = trim( $pPath, '/' );
$query_pos = strpos( $pPath, '?' );
if ( false !== $query_pos ) {
$query = substr( $pPath, $query_pos );
$pPath = substr( $pPath, 0, $query_pos );
$format_length = strlen( '.' . self::FORMAT );
$start = $format_length * ( - 1 ); //negative
if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) {
$pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length );
switch ( $this->_scope ) {
$base = '/apps/' . $this->_id;
$base = '/developers/' . $this->_id;
$base = '/users/' . $this->_id;
$base = '/plugins/' . $this->_id;
$base = '/installs/' . $this->_id;
throw new Freemius_Exception( 'Scope not implemented.' );
return '/v' . FS_API__VERSION . $base .
( ! empty( $pPath ) ? '/' : '' ) . $pPath .
( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query;
abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = array() );
* @return object[]|object|null
private function _Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
$pMethod = strtoupper( $pMethod );
$result = $this->MakeRequest( $pPath, $pMethod, $pParams );
} catch ( Freemius_Exception $e ) {
$result = (object) $e->getResult();
} catch ( Exception $e ) {
$result = (object) array(
'error' => (object) array(
'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams );
* Base64 decoding that does not need to be urldecode()-ed.
* Exactly the same as PHP base64 encode except it uses
* @param string $input Base64UrlEncoded() string
protected static function Base64UrlDecode( $input ) {
* This is a hack suggested by @otto42 and @greenshady from
* the theme's review team. The usage of base64 for API
* signature encoding was approved in a Slack meeting
* held on Tue (10/25 2016).
* @todo Remove this hack once the base64 error is removed from the Theme Check.
* @author Vova Feldman (@svovaf)
$fn = 'base64' . '_decode';
return $fn( strtr( $input, '-_', '+/' ) );
* Base64 encoding that does not need to be urlencode()ed.
* Exactly the same as base64 encode except it uses
* @param string $input string
* @return string Base64 encoded string
protected static function Base64UrlEncode( $input ) {
* This is a hack suggested by @otto42 and @greenshady from
* the theme's review team. The usage of base64 for API
* signature encoding was approved in a Slack meeting
* held on Tue (10/25 2016).
* @todo Remove this hack once the base64 error is removed from the Theme Check.
* @author Vova Feldman (@svovaf)
$fn = 'base64' . '_encode';
$str = strtr( $fn( $input ), '+/', '-_' );
$str = str_replace( '=', '', $str );