Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to change the base REST API prefix
- Loading branch information
1 parent
bac99f9
commit 2c3f660
Showing
8 changed files
with
158 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
if ( ! defined( 'ABSPATH' ) ) die( 'restricted access' ); | ||
|
||
if ( ! class_exists( 'REST_API_Toolbox_Base' ) ) { | ||
|
||
class REST_API_Toolbox_Base { | ||
|
||
var $settings; | ||
|
||
public function __construct() { | ||
$this->settings = new REST_API_Toolbox_Settings(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
if ( ! defined( 'ABSPATH' ) ) die( 'restricted access' ); | ||
|
||
if ( ! class_exists( 'REST_API_Toolbox_Prefix' ) ) { | ||
|
||
class REST_API_Toolbox_Prefix extends REST_API_Toolbox_Base { | ||
|
||
public function plugins_loaded() { | ||
|
||
add_filter( 'rest_url_prefix', array( $this, 'change_url_prefix' ), 100 ); | ||
|
||
} | ||
|
||
public function change_url_prefix( $prefix ) { | ||
|
||
$custom_prefix = $this->settings->setting_get( 'general', 'rest-api-prefix' ); | ||
$prefix = ! empty( $custom_prefix ) ? $custom_prefix : $prefix; | ||
|
||
return $prefix; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
class REST_API_Toolbox_Test_Prefix extends REST_API_Toolbox_Test_Base { | ||
|
||
function test_changed_prefix() { | ||
|
||
$settings = new REST_API_Toolbox_Settings(); | ||
$settings->change_setting( 'general', 'rest-api-prefix', 'hello-world' ); | ||
|
||
$this->assertEquals( 'hello-world', $settings->setting_get( 'general', 'rest-api-prefix' ) ); | ||
|
||
$this->assertEquals( 'hello-world', rest_get_url_prefix() ); | ||
|
||
} | ||
|
||
function test_unchanged_prefix() { | ||
|
||
$settings = new REST_API_Toolbox_Settings(); | ||
$settings->change_setting( 'general', 'rest-api-prefix', '' ); | ||
|
||
$this->assertEquals( '', $settings->setting_get( 'general', 'rest-api-prefix' ) ); | ||
|
||
$this->assertEquals( 'wp-json', rest_get_url_prefix() ); | ||
|
||
} | ||
|
||
function test_sanitized_prefix() { | ||
|
||
$settings = new REST_API_Toolbox_Settings(); | ||
$settings->change_setting( 'general', 'rest-api-prefix', 'he||o /world<' ); // invalid URL, will be sanitized | ||
|
||
$this->assertEquals( 'heo-world', $settings->setting_get( 'general', 'rest-api-prefix' ) ); | ||
|
||
$this->assertEquals( 'heo-world', rest_get_url_prefix() ); | ||
|
||
} | ||
|
||
} |