Skip to content

Commit

Permalink
LICENS-24 Add abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
MlKilderkin committed Dec 7, 2022
1 parent 448f6cb commit 82e9b10
Show file tree
Hide file tree
Showing 18 changed files with 353 additions and 61 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
"StellarWP\\Uplink\\Tests\\": "tests/_support/Helper/"
}
},
"repositories": [
{
"type": "vcs",
"url": "[email protected]:stellarwp/container-contract.git",
"no-api": true
}
],
"require": {
"php": "^7.0 | ^8.0",
"lucatume/di52": "^3.0",
"php": "^7.0 | ^8.0"
"stellarwp/container-contract": "^1.0"
},
"config": {
"preferred-install": "dist",
Expand Down
48 changes: 45 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/Uplink/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\API;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Container;
use StellarWP\Uplink\Resources\Resource;
use StellarWP\Uplink\Site\Data;
Expand Down Expand Up @@ -113,7 +114,7 @@ public function get_api_base_url(): string {
*
* @param string $base_url Base URL.
*/
return apply_filters( 'stellar_uplink_api_get_base_url', static::$base_url );
return apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'api_get_base_url', static::$base_url );
}

/**
Expand Down Expand Up @@ -159,7 +160,7 @@ protected function request( $method, $endpoint, $args ) {
* @param string $endpoint Request method.
* @param array<mixed> $args Request data.
*/
$request_args = apply_filters( 'stellar_uplink_api_request_args', $request_args, $endpoint, $args );
$request_args = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'api_request_args', $request_args, $endpoint, $args );

$url = static::$base_url . static::$api_root . $endpoint;

Expand All @@ -176,7 +177,7 @@ protected function request( $method, $endpoint, $args ) {
* @param string $endpoint API endpoint.
* @param array<mixed> $args API arguments.
*/
$result = apply_filters( 'stellar_uplink_api_response', $result, $endpoint, $args );
$result = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'api_response', $result, $endpoint, $args );

return $result;
}
Expand Down Expand Up @@ -216,7 +217,7 @@ public function validate_license( Resource $resource, string $key = null, string
*
* @param array<mixed> $args License validation arguments.
*/
$args = apply_filters( 'stellar_uplink_client_validate_license_args', $args );
$args = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'client_validate_license_args', $args );

$request_hash = $this->build_hash( $args );
$cache_key = 'stellar_uplink_validate_license_' . $request_hash;
Expand All @@ -240,6 +241,6 @@ public function validate_license( Resource $resource, string $key = null, string
* @param Validation_Response $results License validation results.
* @param array<mixed> $args License validation arguments.
*/
return apply_filters( 'stellar_uplink_client_validate_license', $results, $args );
return apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'client_validate_license', $results, $args );
}
}
5 changes: 3 additions & 2 deletions src/Uplink/Admin/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Container;
use StellarWP\Uplink\Resources\Collection;

Expand Down Expand Up @@ -99,7 +100,7 @@ public function field_html( array $args = [] ): void {
$args['plugin']
);

echo apply_filters( 'stellar_uplink_license_field_html_render', $field, $args );
echo apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'license_field_html_render', $field, $args );

$this->get_description( $args );
}
Expand Down Expand Up @@ -130,7 +131,7 @@ protected function get_content( array $context = [] ) {
* @return string
*/
public function get_path(): string {
return apply_filters( 'stellar_uplink_field-template_path', dirname( __DIR__, 2 ) . $this->path, $this->path );
return apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'field-template_path', dirname( __DIR__, 2 ) . $this->path, $this->path );
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Uplink/Admin/License_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Container;
use StellarWP\Uplink\Resources\Collection;
use StellarWP\Uplink\Resources\Plugin;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function get_field_html( Plugin $plugin ): string {
$html .= '<div class="license-test-results"><img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="ajax-loading-license" alt="Loading" style="display: none"/>';
$html .= '<div class="key-validity"></div></div>';

return apply_filters( 'stellar_uplink_license_field_html', $html, $plugin->get_slug() );
return apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'license_field_html', $html, $plugin->get_slug() );
}

public function render(): void {
Expand All @@ -75,12 +76,12 @@ public function render(): void {
public function enqueue_assets(): void{
$handle = 'stellar-uplink-license-admin';
$path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) );
$js_src = apply_filters( 'stellar_uplink_admin_js_source', $path . '/resources/js/key-admin.js' );
$js_src = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'admin_js_source', $path . '/resources/js/key-admin.js' );

wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true );
wp_enqueue_script( $handle );

$css_src = apply_filters( 'stellar_uplink_admin_css_source', $path . '/resources/css/main.css' );
$css_src = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'admin_css_source', $path . '/resources/css/main.css' );
wp_enqueue_style( 'stellar-uplink-license-admin', $css_src );
}

Expand Down
3 changes: 2 additions & 1 deletion src/Uplink/Admin/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Messages\Expired_Key;
use StellarWP\Uplink\Messages\Unlicensed;

Expand Down Expand Up @@ -92,7 +93,7 @@ public function save_notices(): void {
* @param array $current_notices
* @param array $previously_saved_notices
*/
do_action( 'stellar_uplink_notices_save_notices', $this->notices, $this->saved_notices );
do_action( 'stellar_uplink_' . Config::get_hook_prefix(). 'notices_save_notices', $this->notices, $this->saved_notices );
}

}
7 changes: 4 additions & 3 deletions src/Uplink/Admin/Plugins_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Container;
use StellarWP\Uplink\Messages;
use StellarWP\Uplink\Resources\Collection;
Expand Down Expand Up @@ -125,7 +126,7 @@ public function display_plugin_messages( string $page ): void {
'message_row_html' => $message_row_html,
];

add_filter( 'stellar_uplink_plugin_notices', [ $this, 'add_notice_to_plugin_notices' ] );
add_filter( 'stellar_uplink_' . Config::get_hook_prefix(). 'plugin_notices', [ $this, 'add_notice_to_plugin_notices' ] );
}

/**
Expand All @@ -152,9 +153,9 @@ public function store_admin_notices( string $page ): void {
if ( 'plugins.php' !== $page ) {
return;
}
$notices = apply_filters( 'stellar_uplink_plugin_notices', [] );
$notices = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'plugin_notices', [] );
$path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) );
$js_src = apply_filters( 'stellar_uplink_admin_js_source', $path . '/resources/js/notices.js' );
$js_src = apply_filters( 'stellar_uplink_' . Config::get_hook_prefix(). 'admin_js_source', $path . '/resources/js/notices.js' );
$handle = 'stellar_uplink-notices';

wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true );
Expand Down
58 changes: 45 additions & 13 deletions src/Uplink/Admin/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace StellarWP\Uplink\Admin;

class Provider extends \tad_DI52_ServiceProvider {
use StellarWP\Uplink\Contracts\Abstract_Subscriber;

class Provider extends Abstract_Subscriber {
/**
* Register the service provider.
*
Expand All @@ -20,20 +22,50 @@ public function register() {
}

public function register_hooks(): void {
add_filter( 'plugins_api', $this->container->callback( Plugins_Page::class, 'inject_info' ), 10, 3 );
add_filter( 'plugins_api', function ( $result, $action, $args ) {
return $this->container->get( Plugins_Page::class)->inject_info( $result, $action, $args );
}, 10, 3 );

if ( ( ! defined( 'TRIBE_DISABLE_PUE' ) || true !== TRIBE_DISABLE_PUE ) ) {
add_filter( 'pre_set_site_transient_update_plugins', $this->container->callback( Plugins_Page::class, 'check_for_updates' ) );
add_filter( 'pre_set_site_transient_update_plugins', function ( $transient ) {
return $this->container->get( Plugins_Page::class )->check_for_updates( $transient );
}, 10, 1 );
}
add_action( 'admin_init', $this->container->callback( License_Field::class, 'register_settings' ) );
add_action( 'admin_enqueue_scripts', $this->container->callback( License_Field::class, 'enqueue_assets' ) );
add_action( 'admin_notices', $this->container->callback( Notice::class, 'setup_notices' ) );
add_action( 'wp_ajax_pue-validate-key-uplink' , $this->container->callback( Ajax::class, 'validate_license' ) );
add_action( 'admin_enqueue_scripts', $this->container->callback( Plugins_Page::class, 'display_plugin_messages' ), 1 );
add_action( 'admin_enqueue_scripts', $this->container->callback( Plugins_Page::class, 'store_admin_notices' ) );
add_action( 'load-plugins.php', $this->container->callback( Plugins_Page::class, 'remove_default_inline_update_msg' ), 50 );

add_filter( 'upgrader_pre_download', $this->container->callback( Package_Handler::class, 'filter_upgrader_pre_download' ) , 5, 3 );
add_filter( 'upgrader_source_selection', $this->container->callback( Update_Prevention::class, 'filter_upgrader_source_selection' ) , 15, 4 );

add_action( 'admin_init', function() {
$this->container->get( License_Field::class)->register_settings();
}, 10, 0 );

add_action( 'admin_enqueue_scripts', function () {
$this->container->get( License_Field::class )->enqueue_assets();
}, 10, 0 );

add_action( 'admin_notices', function () {
$this->container->get( Notice::class )->setup_notices();
}, 10, 0 );

add_action( 'wp_ajax_pue-validate-key-uplink' , function () {
$this->container->get( Ajax::class)->validate_license();
}, 10, 0 );

add_action( 'admin_enqueue_scripts', function ( $page ) {
$this->container->get( Plugins_Page::class)->display_plugin_messages( $page );
}, 1, 1 );

add_action( 'admin_enqueue_scripts', function ( $page ) {
$this->container->get( Plugins_Page::class )->store_admin_notices( $page );
}, 10, 0 );

add_action( 'load-plugins.php', function () {
$this->container->get( Plugins_Page::class )->remove_default_inline_update_msg();
}, 50, 0 );

add_filter( 'upgrader_pre_download', function ( $reply, $package, $upgrader ) {
return $this->container->get( Package_Handler::class )->filter_upgrader_pre_download( $reply, $package, $upgrader );
}, 5, 3 );

add_filter( 'upgrader_source_selection', function ( $source, $remote_source, $upgrader, $extras ) {
return $this->container->callback( Update_Prevention::class )->filter_upgrader_source_selection( $source, $remote_source, $upgrader, $extras );
}, 15, 4 );
}
}
5 changes: 3 additions & 2 deletions src/Uplink/Admin/Update_Prevention.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Container;
use StellarWP\Uplink\Resources\Collection;
use StellarWP\Uplink\Resources\Plugin;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function filter_upgrader_source_selection( string $source, $remote_source
}

$incompatible_plugins = apply_filters(
'stellar_uplink_update_prevention_incompatible_plugins',
'stellar_uplink_' . Config::get_hook_prefix(). 'update_prevention_incompatible_plugins',
[],
$source,
$remote_source
Expand All @@ -88,7 +89,7 @@ public function filter_upgrader_source_selection( string $source, $remote_source
* @param array $extra Extra arguments passed to hooked filters.
*/
$should_prevent_update = apply_filters(
'stellar_uplink_should_prevent_update_without_license',
'stellar_uplink_' . Config::get_hook_prefix(). 'should_prevent_update_without_license',
true,
$plugin,
$incompatible_plugins,
Expand Down
Loading

0 comments on commit 82e9b10

Please sign in to comment.