Skip to content

Commit

Permalink
Support manual asset paths (#45)
Browse files Browse the repository at this point in the history
* Support a direct path to static assets

* Fix composer.json formatting

* provide part of the path
  • Loading branch information
chrisvanpatten authored Jan 27, 2020
1 parent 9261a10 commit 33982b8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"config": {
"sort-packages": true
"sort-packages": true
},
"require-dev": {
"automattic/vipwpcs": "^0.4.0",
Expand Down
25 changes: 20 additions & 5 deletions src/class-wordpress-options-panels.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ class WordPress_Options_Panels {
* @param string $plugins_installed_dir Install directory of the plugin.
* @param string $plugins_installed_url Install plugin URL.
* @param string $plugin_basedir Base plugin directory.
* @param string $asset_dir_url A direct URL path to the static assets.
*/
public function __construct( $plugins_installed_dir, $plugins_installed_url, $plugin_basedir ) {
$current_dir = dirname( __FILE__ );
$relative_dir = str_replace( $plugin_basedir . '/', '', $current_dir );
public function __construct(
$plugins_installed_dir,
$plugins_installed_url,
$plugin_basedir,
$asset_dir_url = null
) {
$current_dir = dirname( __FILE__ );
$relative_dir = str_replace( $plugin_basedir . '/', '', $current_dir );

$this->installed_dir = $plugin_basedir . '/' . $relative_dir;
$this->installed_url = $plugins_installed_url . $relative_dir;
$this->asset_dir_url = $asset_dir_url;

// Data api wrappers.
foreach ( glob( trailingslashit( dirname( __FILE__ ) ) . 'inc/api/class-*.php' ) as $file ) {
include_once $file;
Expand Down Expand Up @@ -81,8 +90,14 @@ public function __construct( $plugins_installed_dir, $plugins_installed_url, $pl
* @return \WPOP\V_5_0\Page
*/
public function register_page( $options_page_slug, $options_page_type, $parent_menu_id = null ) {

return new Page( $options_page_slug, $options_page_type, $parent_menu_id, $this->installed_dir, $this->installed_url );
return new Page(
$options_page_slug,
$options_page_type,
$parent_menu_id,
$this->installed_dir,
$this->installed_url,
$this->asset_dir_url
);
}

}
26 changes: 21 additions & 5 deletions src/inc/class-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,22 @@ class Page {
* @param string|null $parent_menu_id The parent id only set if this is a sub_menu.
* @param string $installed_dir A field value of menu slug.
* @param string $installed_dir_uri A value of either main_menu or sub_menu.
* @param string $asset_dir_url A direct path to static assets.
*/
public function __construct( $options_page_slug, $options_page_type, $parent_menu_id, $installed_dir, $installed_dir_uri ) {
public function __construct(
$options_page_slug,
$options_page_type,
$parent_menu_id,
$installed_dir,
$installed_dir_uri,
$asset_dir_url = null
) {
$this->slug = $options_page_slug;
$this->type = $options_page_type;
$this->parent_menu_id = $parent_menu_id;
$this->installed_dir = $installed_dir;
$this->installed_dir_uri = $installed_dir_uri;
$this->asset_dir_url = $asset_dir_url;
}

/**
Expand Down Expand Up @@ -239,16 +248,23 @@ public function build_parts() {
* @return void
*/
public function maybe_run_footer_scripts( $screen ) {
$flag1 = stristr( $screen->id, $this->slug );
$flag2 = $this->installed_dir_uri;
if ( false === stristr( $screen->id, $this->slug ) || null === $this->installed_dir_uri ) {
if ( false === stristr( $screen->id, $this->slug ) ) {
return;
}

// Either asset_dir_url or installed_dir_uri must be set.
if ( null === $this->asset_dir_url || null === $this->installed_dir_uri ) {
return;
}

$asset_class_path = __NAMESPACE__ . '\\Assets';

if ( null !== $this->asset_dir_url ) {
$this->asset_dir_url = $this->asset_dir_url . '/wordpress-phoenix/wordpress-options-builder-class/src';
}

// Instantiate the Asset class with the installed directory as a parameter.
$asset_class = new $asset_class_path( $this->installed_dir_uri );
$asset_class = new $asset_class_path( $this->asset_dir_url ?? $this->installed_dir_uri );

add_action(
'admin_print_footer_scripts-' . $screen->id,
Expand Down
2 changes: 2 additions & 0 deletions src/inc/page-parts/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Class Assets
*/
class Assets {

/**
* Installed Directory URI
*
Expand Down Expand Up @@ -199,4 +200,5 @@ public function inline_js_footer() {
</script>
<?php
}

}

0 comments on commit 33982b8

Please sign in to comment.