Skip to content

Commit

Permalink
Merge pull request #75 from code-soup/vb/dev
Browse files Browse the repository at this point in the history
Fix Assets Class
  • Loading branch information
Bobz-zg authored Apr 18, 2024
2 parents b8b3dbd + 98b8aa6 commit e2532ed
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 45 deletions.
15 changes: 3 additions & 12 deletions includes/admin/class-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ public function __construct() {
$this->assets = $instance->get_assets();

// Admin hooks.
$hooker->add_action( 'admin_enqueue_scripts', $this, 'enqueue_styles' );
$hooker->add_action( 'admin_enqueue_scripts', $this, 'enqueue_scripts' );
$hooker->add_action( 'admin_enqueue_scripts', $this );
}

/**
* Enqueue the stylesheets for wp-admin.
* Register the CSS/JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles() {
public function admin_enqueue_scripts() {

wp_enqueue_style(
$this->get_plugin_id('/wp/css'),
Expand All @@ -56,14 +55,6 @@ public function enqueue_styles() {
$this->get_plugin_version(),
'all'
);
}

/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts() {

$script_id = $this->get_plugin_id('/wp/js');

Expand Down
54 changes: 23 additions & 31 deletions includes/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ class Assets {
private $manifest;


/**
* Manifest file object containing list of all hashed assets
*
* @var Object
*/
private $manifest_path;


/**
* Absolut path to theme 'dist' folder
*
* @var string
*/
private $dist_path;


/**
* URI to theme 'dist' folder
*
Expand All @@ -51,23 +35,31 @@ class Assets {
*/
public function __construct() {

$this->dist_uri = $this->get_plugin_dir_url('/dist');
$this->dist_path = $this->get_plugin_dir_path('/dist');
$this->manifest_path = $this->get_plugin_dir_path('/dist/assets.json');
$this->manifest = array();
$this->dist_uri = $this->get_plugin_dir_url('/dist');
$manifest_path = $this->get_plugin_dir_path('/dist/assets.json');

/**
* Use built in WordPress classes
*/
if ( ! defined('FS_CHMOD_DIR') ) {
define( 'FS_CHMOD_DIR', ( 0755 & ~ umask() ) );
}

if ( file_exists($this->manifest_path) )
{
$response = wp_remote_get($this->manifest_path);

if (is_wp_error($response)) {
$error_message = $response->get_error_message();
error_log("Error retrieving manifest: $error_message");
}
else {
$this->manifest = json_decode(wp_remote_retrieve_body($response), true);
}
if ( ! defined('FS_CHMOD_FILE') ) {
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
}

require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

$fs = new \WP_Filesystem_Direct('');

/**
* Test for assets.json
*/
$this->manifest = $fs->exists($manifest_path)
? json_decode( $fs->get_contents($manifest_path), true )
: array();
}


Expand Down
36 changes: 36 additions & 0 deletions includes/class-hooker.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ public function add_action( $hook, $component, $callback = '', $priority = 10, $
);
}


/**
* Add array of actions at once
*/
public function add_actions( array $actions = [] ) {

foreach( $actions as $action )
{
$hook = isset($action[0]) ? $action[0] : '';
$component = isset($action[1]) ? $action[1] : '';
$callback = isset($action[2]) ? $action[2] : '';
$priority = isset($action[3]) ? $action[3] : 10;
$accepted_arg = isset($action[4]) ? $action[4] : 1;

$this->add_action( $hook, $component, $callback = '', $priority = 10, $accepted_args = 1 );
}
}

/**
* Add a new filter to the collection to be registered with WordPress.
*
Expand Down Expand Up @@ -99,6 +117,24 @@ public function add_filter( $hook, $component, $callback = '', $priority = 10, $
);
}


/**
* Add array of filters at once
*/
public function add_filters( array $filters = [] ) {

foreach( $filters as $filter )
{
$hook = isset($filter[0]) ? $filter[0] : '';
$component = isset($filter[1]) ? $filter[1] : '';
$callback = isset($filter[2]) ? $filter[2] : '';
$priority = isset($filter[3]) ? $filter[3] : 10;
$accepted_arg = isset($filter[4]) ? $filter[4] : 1;

$this->add_filter( $hook, $component, $callback = '', $priority = 10, $accepted_args = 1 );
}
}

/**
* A utility function that is used to register the actions and hooks into a single
* collection.
Expand Down
6 changes: 4 additions & 2 deletions includes/frontend/class-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public function __construct() {
$hooker = $instance->get_hooker();
$this->assets = $instance->get_assets();

$hooker->add_action( 'wp_enqueue_scripts', $this, 'enqueue_styles' );
$hooker->add_action( 'wp_enqueue_scripts', $this, 'enqueue_scripts' );
$hooker->add_actions([
['wp_enqueue_scripts', $this, 'enqueue_styles'],
['wp_enqueue_scripts', $this, 'enqueue_scripts']
]);
}


Expand Down

0 comments on commit e2532ed

Please sign in to comment.