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

Bugfix: dynamic asset loading #47

Merged
merged 5 commits into from
Nov 29, 2023
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
14 changes: 7 additions & 7 deletions src/Uplink/Admin/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ abstract class Field {
*
* @var string
*/
protected string $path = '';
protected $path = '';

/**
* @since 1.0.0
*
* @return void
*/
abstract public function register_settings();
abstract public function register_settings(): void;

/**
* @param array<string> $args
*
* @return void
*/
public function get_description( array $args = [] ) {
public function get_description( array $args = [] ): void {
if ( empty( $args['description'] ) ) {
return;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public function get_group_name( string $group_modifier = '' ) : string {
*
* @return void
*/
public function field_html( array $args = [] ) {
public function field_html( array $args = [] ): void {
$field = sprintf(
'<div class="%6$s" id="%2$s" data-slug="%2$s" data-plugin="%9$s" data-plugin-slug="%10$s">
<fieldset class="stellarwp-uplink__settings-group">
Expand Down Expand Up @@ -112,7 +112,7 @@ public function add_nonce_field() : string {
*
* @return void
*/
abstract public function render( bool $show_title = true, bool $show_button = true );
abstract public function render( bool $show_title = true, bool $show_button = true ): void;

/**
* @param array<mixed> $context
Expand Down Expand Up @@ -155,11 +155,11 @@ protected function get_plugin() {
* @since 1.0.0
*
* @param string $page Slug title of the admin page whose settings fields you want to show.
* @param string $page Slug title of the admin page whose settings fields you want to show.
* @param string $section Slug title of the settings section whose fields you want to show.
* @param string $plugin_slug Slug title of the settings section whose fields you want to show.
* @param bool $show_title Whether to show the title or not.
*/
public function do_settings_fields( string $page, string $section, string $plugin_slug, bool $show_title = true ) {
public function do_settings_fields( string $page, string $section, string $plugin_slug, bool $show_title = true ): void {
global $wp_settings_fields;

if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) {
Expand Down
43 changes: 31 additions & 12 deletions src/Uplink/Admin/License_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Resources\Collection;
use StellarWP\Uplink\Resources\Plugin;
use StellarWP\Uplink\Uplink;

class License_Field extends Field {

public const LICENSE_FIELD_ID = 'stellarwp_uplink_license';

protected string $path = '/admin-views/fields/settings.php';
/**
* @var string
*/
protected $path = '/admin-views/fields/settings.php';

/**
* The script and style handle when registering assets for this field.
*
* @var string
*/
private $handle;
/**
* Constructor. Initializes handle.
*/
public function __construct() {
$this->handle = sprintf( 'stellarwp-uplink-license-admin-%s', Config::get_hook_prefix() );
}

/**
* @param Plugin $plugin
Expand All @@ -26,7 +43,7 @@ public function get_section_name( Plugin $plugin ) : string {
*
* @return void
*/
public function register_settings() {
public function register_settings(): void {
$collection = Config::get_container()->get( Collection::class );
$plugin = $collection->current();

Expand Down Expand Up @@ -77,7 +94,10 @@ public function get_field_html( Plugin $plugin ) : string {
/**
* @inheritDoc
*/
public function render( bool $show_title = true, bool $show_button = true ) {
public function render( bool $show_title = true, bool $show_button = true ): void {
wp_enqueue_script( $this->handle );
wp_enqueue_style( $this->handle );

echo $this->get_content( [
'plugin' => $this->get_plugin(),
'show_title' => $show_title,
Expand All @@ -90,16 +110,15 @@ public function render( bool $show_title = true, bool $show_button = true ) {
*
* @return void
*/
public function enqueue_assets() {
$handle = sprintf( 'stellarwp-uplink-license-admin-%s', Config::get_hook_prefix() );
$path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) );
$js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/assets/js/key-admin.js' );
wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true );
wp_enqueue_script( $handle );
public function enqueue_assets(): void {
$path = Config::get_container()->get( Uplink::UPLINK_ASSETS_URI );
$js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/js/key-admin.js' );
wp_register_script( $this->handle, $js_src, [ 'jquery' ], '1.0.0', true );

$action_postfix = Config::get_hook_prefix_underscored();
wp_localize_script( $handle, sprintf( 'stellarwp_config_%s', $action_postfix ), [ 'action' => sprintf( 'pue-validate-key-uplink-%s', $action_postfix ) ] );
wp_localize_script( $this->handle, sprintf( 'stellarwp_config_%s', $action_postfix ), [ 'action' => sprintf( 'pue-validate-key-uplink-%s', $action_postfix ) ] );

$css_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_css_source', $path . '/assets/css/main.css' );
wp_enqueue_style( $handle, $css_src );
$css_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_css_source', $path . '/css/main.css' );
wp_register_style( $this->handle, $css_src );
}
}
20 changes: 10 additions & 10 deletions src/Uplink/Admin/Plugins_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Plugins_Page {
*
* @var array<mixed>
*/
public array $plugin_notice = [];
public $plugin_notice = [];

/**
* Displays messages on the plugins page in the dashboard.
Expand All @@ -26,7 +26,7 @@ class Plugins_Page {
*
* @return void
*/
public function display_plugin_messages( string $page ) {
public function display_plugin_messages( string $page ): void {
if ( 'plugins.php' !== $page ) {
return;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function get_plugin_notice() {
*
* @return void
*/
public function store_admin_notices( string $page ) {
public function store_admin_notices( string $page ): void {
if ( 'plugins.php' !== $page ) {
return;
}
Expand All @@ -141,8 +141,8 @@ public function store_admin_notices( string $page ) {
*
* @return void
*/
public function output_notices_script() {
$slug = $this->get_plugin()->get_slug();
public function output_notices_script(): void {
$slug = $this->get_plugin()->get_slug();
$notice = $this->get_plugin_notice();

if ( empty( $notice ) ) {
Expand Down Expand Up @@ -203,7 +203,7 @@ public function get_plugin_message( Plugin $resource ) {
*
* @return void
*/
public function remove_default_inline_update_msg() {
public function remove_default_inline_update_msg(): void {
remove_action( "after_plugin_row_{$this->get_plugin()->get_path()}", 'wp_plugin_update_row' );
}

Expand Down Expand Up @@ -235,13 +235,13 @@ protected function get_plugin() {
*
* @see plugins_api()
*
* @param mixed $result
* @param string $action
* @param array<mixed>|object $args
* @param mixed $result
* @param string|null $action
* @param array<mixed>|object|null $args
*
* @return mixed
*/
public function inject_info( $result, string $action = null, $args = null ) {
public function inject_info( $result, ?string $action = null, $args = null ) {
$relevant = ( 'plugin_information' === $action ) && is_object( $args ) && isset( $args->slug ) && ( $args->slug === $this->get_plugin()->get_slug() );

if ( ! $relevant ) {
Expand Down
3 changes: 3 additions & 0 deletions src/Uplink/Uplink.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Uplink {

public const UPLINK_ASSETS_URI = 'uplink.assets.uri';

/**
* Initializes the service provider.
*
Expand All @@ -23,6 +25,7 @@ public static function init() {

$container = Config::get_container();

$container->singleton( self::UPLINK_ASSETS_URI, dirname( plugin_dir_url( __FILE__ ) ) . '/assets' );
$container->bind( ContainerInterface::class, $container );
$container->singleton( View\Provider::class, View\Provider::class );
$container->singleton( API\Client::class, API\Client::class );
Expand Down
18 changes: 14 additions & 4 deletions tests/wpunit/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<?php
<?php declare( strict_types=1 );

namespace wpunit;

use StellarWP\ContainerContract\ContainerInterface;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Tests\UplinkTestCase;
use StellarWP\Uplink\Uplink;

class ContainerTest extends UplinkTestCase {

/**
* Test that the container is correctly instantiated.
*
* @test
*/
public function it_should_instantiate() {
public function test_it_should_instantiate(): void {
$container = Config::get_container();

$this->assertInstanceOf( ContainerInterface::class, $container );
}

public function test_it_gets_the_uplink_assets_uri(): void {
$uri = $this->container->get( Uplink::UPLINK_ASSETS_URI );

$this->assertSame( 'http://wordpress.test/wp-content/plugins/uplink/src/assets', $uri );

$uri = Config::get_container()->get( Uplink::UPLINK_ASSETS_URI );

$this->assertSame( 'http://wordpress.test/wp-content/plugins/uplink/src/assets', $uri );
}
}
Loading