Skip to content

Commit

Permalink
Merge pull request #38 from connorjburton/version-improvement
Browse files Browse the repository at this point in the history
Improving version detection
  • Loading branch information
Chris Hutchinson authored Apr 11, 2017
2 parents e3b3cc0 + 9cebb76 commit 429b489
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions acf-to-wp-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function __construct() {
$this->plugin->url = WP_PLUGIN_URL . '/' . str_replace(basename( __FILE__), "", plugin_basename(__FILE__));
$this->plugin->version = '1.3.3';

$this->apiVersion = (REST_API_VERSION) ?: get_option( 'rest_api_plugin_version', get_option( 'json_api_plugin_version', null ) );
$this->apiVersion = $this->_getAPIVersion();

// Version One
if($this->_isAPIVersionOne()) {
Expand Down Expand Up @@ -107,6 +107,26 @@ private function _versionTwoSetup() {
add_action( 'rest_api_init', array( $this, 'addACFOptionRouteV2') );
}

/**
* Returns the WP REST API version, assumes version 2
* if can't find any other version
*
* @return string The version number, set by WP REST API
*
* @since 1.3.2
*/
private function _getAPIVersion() {
$version = 2;

if ( defined('REST_API_VERSION') ) {
$version = REST_API_VERSION;
} else {
$version = get_option( 'rest_api_plugin_version', get_option( 'json_api_plugin_version', null ) );
}

return $version;
}

/**
* Gets the version number of the WP REST API
*
Expand All @@ -123,14 +143,10 @@ private function _getAPIBaseVersion() {
return false;
}

$baseNumber = substr( $version, 0, 1 );

if( $baseNumber === '1' ) {
return 1;
}
$baseNumber = (int) substr( $version, 0, 1 );

if( $baseNumber === '2' ) {
return 2;
if( $baseNumber > 0 ) {
return $baseNumber;
}

return false;
Expand Down

0 comments on commit 429b489

Please sign in to comment.