Last active
September 3, 2022 00:15
-
-
Save afragen/ba3fc125fb0ecd242b1b162153d6adf9 to your computer and use it in GitHub Desktop.
Add to Dependencies tab via Git Updater PRO REST API
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
<?php | |
/** | |
* Add Plugin Dependency via REST API. | |
* | |
* @package Fragen\Plugin_Dependency_API | |
* | |
* Plugin Name: Add Plugin Dependency via REST API | |
* Plugin URI: https://gist.github.com/afragen/ba3fc125fb0ecd242b1b162153d6adf9 | |
* Description: Add plugin dependency data to Dependencies tab via REST API endpoint and allow for Install Now button to function. | |
* Version: 0.11.3 | |
* Author: Andy Fragen | |
* License: MIT | |
* Requires at least: 6.0 | |
* Requires PHP: 7.1 | |
* Gist Plugin URI: https://gist.github.com/afragen/ba3fc125fb0ecd242b1b162153d6adf9 | |
* | |
* An example plugins_api(), response https://git-updater.com/wp-json/git-updater/v1/plugins-api/?slug=git-updater | |
*/ | |
namespace Fragen\Plugin_Dependency_API; | |
/* | |
* Exit if called directly. | |
* PHP version check and exit. | |
*/ | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
// Add the sites with REST enpoints that return plugins_api() data when passed `slug` query arg. | |
add_filter( | |
'plugin_dependency_endpoints', | |
function () { | |
return [ | |
'https://git-updater.com/wp-json/git-updater/v1/plugins-api/', | |
]; | |
} | |
); | |
/** | |
* Class for integration of non-dot org plugin dependencies and WP core. | |
*/ | |
class REST_Endpoints { | |
/** | |
* Load hooks, let's get going. | |
* | |
* @return void | |
*/ | |
public function load_hooks() { | |
add_filter( 'plugins_api_result', [ $this, 'plugins_api_result' ], 10, 3 ); | |
add_filter( 'upgrader_post_install', [ $this, 'upgrader_post_install' ], 10, 3 ); | |
} | |
/** | |
* Filter `plugins_api_result` for adding plugin dependencies. | |
* | |
* @param \stdClass $response Response from `plugins_api()`. | |
* @param string $action Action type. | |
* @param \stdClass $args Array of data from hook. | |
* | |
* @return void|\WP_Error | |
*/ | |
public function plugins_api_result( $response, $action, $args ) { | |
if ( is_wp_error( $response ) ) { | |
/** | |
* Filter the REST enpoints used for lookup of plugins API data. | |
* | |
* @param array | |
*/ | |
$rest_endpoints = apply_filters( 'plugin_dependency_endpoints', [] ); | |
foreach ( $rest_endpoints as $endpoint ) { | |
$url = add_query_arg( 'slug', $args->slug, trailingslashit( $endpoint ) ); | |
$response = wp_remote_get( $url ); | |
$response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
if ( null === $response || isset( $response->error ) || isset( $response->code ) ) { | |
$message = isset( $response->error ) ? $response->error : null; | |
return new \WP_Error( 'error', 'Error retrieving plugin data.', $message ); | |
} | |
break; | |
} | |
// Add slug to hook_extra. | |
add_filter( | |
'upgrader_package_options', | |
function ( $options ) use ( $args ) { | |
$options['hook_extra']['slug'] = $args->slug; | |
return $options; | |
}, | |
10, | |
1 | |
); | |
} | |
return (object) $response; | |
} | |
/** | |
* Filter `upgrader_post_install` for plugin dependencies. | |
* | |
* @uses Git Updater PRO REST API. | |
* | |
* @param bool $true Default is true. | |
* @param array $hook_extra Array of data from hook. | |
* @param array $result Array of data for installation. | |
* | |
* @return bool | |
*/ | |
public function upgrader_post_install( $true, $hook_extra, $result ) { | |
global $wp_filesystem; | |
if ( ! isset( $hook_extra['slug'] ) ) { | |
return $true; | |
} | |
$from = untrailingslashit( $result['destination'] ); | |
$to = trailingslashit( $result['local_destination'] ) . $hook_extra['slug']; | |
if ( $from !== $to ) { | |
// TODO: use move_dir(). | |
if ( ! rename( $from, $to ) ) { | |
$wp_filesystem->mkdir( $to ); | |
copy_dir( $from, $to, [ basename( $to ) ] ); | |
$wp_filesystem->delete( $from, true ); | |
} | |
} | |
return $true; | |
} | |
} | |
( new REST_Endpoints() )->load_hooks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment