Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locally cache host in SP_API #55

Merged
merged 1 commit into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/class-sp-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

class SP_API extends SP_Singleton {

public $connection;

public $host;

public $index = '';

public $type = 'post';

public $request_defaults = array();

public $last_request;
Expand All @@ -24,6 +20,7 @@ class SP_API extends SP_Singleton {
public function setup() {
$url = get_site_url();
$this->index = preg_replace( '#^.*?//(.*?)/?$#', '$1', $url );
$this->host = SP_Config()->get_setting( 'host' );
$this->request_defaults = array(
'sslverify' => false,
'timeout' => 10,
Expand Down Expand Up @@ -99,13 +96,13 @@ public function parse_url( $url = '' ) {
if ( is_string( $url ) ) {
if ( preg_match( '#^https?://#i', $url ) ) {
return $url;
} elseif ( '/' == substr( $url, 0, 1 ) ) {
return SP_Config()->get_setting( 'host' ) . $url;
} elseif ( '/' === substr( $url, 0, 1 ) ) {
return $this->host . $url;
}
}

$defaults = array(
'host' => SP_Config()->get_setting( 'host' ),
'host' => $this->host,
'index' => $this->index,
);

Expand Down
14 changes: 14 additions & 0 deletions lib/class-sp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,22 @@ public function update_settings( $new_settings = array() ) {
if ( ! $this->settings ) {
$this->get_settings();
}
$old_settings = $this->settings;
$this->settings = wp_parse_args( $new_settings, $this->settings );
update_option( 'sp_settings', $this->settings );

if ( ! empty( $new_settings['host'] ) ) {
SP_API()->host = $this->get_setting( 'host' );
}

/**
* Fires after the settings have been updated.
*
* @param array $settings The final settings.
* @param array $new_settings The settings being updated.
* @param array $old_settings The old settings.
*/
do_action( 'sp_config_update_settings', $this->settings, $new_settings, $old_settings );
}
}

Expand Down