Skip to content

Commit

Permalink
Fix plugin updator
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Jun 13, 2019
1 parent 6102636 commit 737e670
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/Kunoichi/Hiden/API/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ protected function endpoint() {
}
return untrailingslashit( $endpoint );
}

/**
* Detect if
*
* @param string $url
*
* @return bool
*/
public function is_kunoichi_url( $url ) {
foreach ( [ $this->endpoint(), 'kunoichiwp.com' ] as $haystack ) {
if ( false !== strpos( $url, $haystack ) ) {
return true;
}
}
return false;
}

/**
* Get plugin endpoint.
Expand Down
21 changes: 16 additions & 5 deletions src/Kunoichi/Hiden/API/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get_plugin_list( $plugins ) {
if ( is_null( $response_json ) ) {
return $this->invalid_error();
}
return $response_json;
return array_map( [ $this, 'object_to_assocs' ], $response_json );
}

/**
Expand All @@ -71,7 +71,18 @@ public function get_plugin_detail( $slug ) {
if ( ! $data ) {
return $this->invalid_error();
}
foreach ( $data as $key => $value ) {
return $this->object_to_assocs( $data );
}

/**
* Convert JSON properties to array.
*
* @param \stdClass $json
*
* @return \stdClass
*/
private function object_to_assocs( $json ) {
foreach ( $json as $key => $value ) {
if ( ! is_object( $value ) ) {
continue;
}
Expand All @@ -81,14 +92,14 @@ public function get_plugin_detail( $slug ) {
array_walk( $value, function( &$value, $key ) {
$value = (array) $value;
} );
$data->{$key} = $value;
$json->{$key} = $value;
break;
default:
$data->{$key} = (array) $value;
$json->{$key} = (array) $value;
break;
}
}
return $data;
return $json;
}

/**
Expand Down
43 changes: 37 additions & 6 deletions src/Kunoichi/Hiden/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function init() {
// add_filter( 'plugins_api_result', [ $this, 'plugins_api_result' ], 10, 3 );
// add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'pre_set_site_transient_update_plugins' ] );
// Filter upgrader source.
// add_filter( 'upgrader_source_selection', [ $this, 'upgrader_source_selection' ], 1 );
add_filter( 'upgrader_source_selection', [ $this, 'upgrader_source_selection' ], 10, 4 );
// Filter plugin API.
add_filter( 'plugins_api', [ $this->plugins, 'plugins_api' ], 10, 3 );
// Filter plugin list.
Expand All @@ -47,6 +47,9 @@ protected function init() {
* @return $plugins
*/
public function site_transient_update_plugins( $plugins ) {
if ( ! $plugins ) {
return $plugins;
}
$list = get_site_transient( $this->plugin_transient );
if ( false === $list ) {
$should_check = $this->plugins->grab_plugins();
Expand Down Expand Up @@ -88,11 +91,39 @@ public function site_transient_update_plugins( $plugins ) {
}
return $plugins;
}



public function upgrader_source_selection() {


/**
* Filter upgrade resource to change directory name.
*
* @param $source
* @param $remote_source
* @param $upgrader
* @param $args
*
* @return mixed
*/
public function upgrader_source_selection( $source, $remote_source, $upgrader, $args ) {
// Check if this plugin is kunoichi?
$files = glob( $source . '*.php' );
if ( $files ) {
foreach ( $files as $file ) {
$info = get_plugin_data( $file );
if ( empty( $info['PluginURI'] ) || ! $this->plugins->is_kunoichi_url( $info['PluginURI'] ) ) {
continue;
}
// This is kunoichi plugin.
// Rename directory and move it to plugin dir.
$path_parts = pathinfo( $source );
$slug = preg_split( '/-\d+\.\d+\.\d+-/u', basename( $source ) );
if ( 2 > count( $slug ) ) {
continue;
}
$new_source = trailingslashit( $path_parts['dirname'] ) . trailingslashit( $slug[0] );
rename( $source, $new_source );
$source = $new_source;
}
}
return $source;
}

/**
Expand Down

0 comments on commit 737e670

Please sign in to comment.