diff --git a/src/Kunoichi/Hiden/API/Endpoint.php b/src/Kunoichi/Hiden/API/Endpoint.php index 7901b96..f118e5b 100644 --- a/src/Kunoichi/Hiden/API/Endpoint.php +++ b/src/Kunoichi/Hiden/API/Endpoint.php @@ -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. diff --git a/src/Kunoichi/Hiden/API/Plugins.php b/src/Kunoichi/Hiden/API/Plugins.php index 670f379..0f00929 100644 --- a/src/Kunoichi/Hiden/API/Plugins.php +++ b/src/Kunoichi/Hiden/API/Plugins.php @@ -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 ); } /** @@ -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; } @@ -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; } /** diff --git a/src/Kunoichi/Hiden/Updater.php b/src/Kunoichi/Hiden/Updater.php index 3c450cc..8edbf79 100644 --- a/src/Kunoichi/Hiden/Updater.php +++ b/src/Kunoichi/Hiden/Updater.php @@ -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. @@ -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(); @@ -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; } /**