Skip to content

Commit

Permalink
Support using module URL outside plugins folder during development
Browse files Browse the repository at this point in the history
  • Loading branch information
eliot-akira committed Feb 21, 2025
1 parent 8fcff89 commit 42dc161
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 94 deletions.
179 changes: 90 additions & 89 deletions enqueue.php
Original file line number Diff line number Diff line change
@@ -1,89 +1,90 @@
<?php

defined('ABSPATH') or die();

/**
* Store the $args of every registered field
*
* It will be passed to JS in order to init our react fields
*/
$fields->enqueued = [
'fields' => [],
'elements' => []
];

$fields->is_enqueued = false;

$fields->enqueue_item = function(
string $name,
string $type,
array $args
) use($fields) : void {

$args['context'] = $fields->current_context;

$fields->enqueued[ $type ][ $name ] = $args;
};

$fields->enqueue = function(array $config = []) use($fields) {

if( ! empty($config['context']) && is_array($config['context']) ) {
$fields->enqueued_contexts = [
...$fields->enqueued_contexts,
...$config['context']
];
}

$contexts = ! empty($fields->enqueued_contexts)
? $fields->enqueued_contexts
: ['default'];

foreach( $contexts as $context ) {

wp_enqueue_style(
'tangible-fields-' . $context,
plugins_url( '/assets', __FILE__ ) . '/build/' . $context . '/index.min.css',
[],
$fields->version
);
}

wp_enqueue_script(
'tangible-fields',
plugins_url( '/assets', __FILE__ ) . '/build/index.min.js',
[ 'wp-element' ],
$fields->version,
true
);

$data = [
'api' => [
'nonce' => wp_create_nonce( 'wp_rest' ),
'endpoint' => [
'media' => esc_url_raw( rest_url( '/wp/v2/media/' ) ),
],
],
'fields' => $fields->enqueued['fields'],
'elements' => $fields->enqueued['elements'],
'dynamics' => $fields->get_dynamic_value_data(),
'mimetypes' => get_allowed_mime_types()
];

wp_add_inline_script( 'tangible-fields', 'var TangibleFields = ' . json_encode($data) . ';', 'before' );

$fields->is_enqueued = true;
};

$fields->maybe_enqueue_scripts = function() use($fields) : void {

$has_registrations = empty($fields->enqueued['fields']) && empty($fields->enqueued['elements']);

if( $has_registrations || $fields->is_enqueued ) {
return;
}

$fields->enqueue();
};

add_action( 'wp_footer', $fields->maybe_enqueue_scripts );
add_action( 'admin_footer', $fields->maybe_enqueue_scripts );
<?php
use tangible\framework;

defined('ABSPATH') or die();

/**
* Store the $args of every registered field
*
* It will be passed to JS in order to init our react fields
*/
$fields->enqueued = [
'fields' => [],
'elements' => []
];

$fields->is_enqueued = false;

$fields->enqueue_item = function(
string $name,
string $type,
array $args
) use($fields) : void {

$args['context'] = $fields->current_context;

$fields->enqueued[ $type ][ $name ] = $args;
};

$fields->enqueue = function(array $config = []) use($fields) {

if( ! empty($config['context']) && is_array($config['context']) ) {
$fields->enqueued_contexts = [
...$fields->enqueued_contexts,
...$config['context']
];
}

$contexts = ! empty($fields->enqueued_contexts)
? $fields->enqueued_contexts
: ['default'];

foreach( $contexts as $context ) {

wp_enqueue_style(
'tangible-fields-' . $context,
framework\module_url( '/assets', __FILE__ ) . '/build/' . $context . '/index.min.css',
[],
$fields->version
);
}

wp_enqueue_script(
'tangible-fields',
framework\module_url( '/assets', __FILE__ ) . '/build/index.min.js',
[ 'wp-element' ],
$fields->version,
true
);

$data = [
'api' => [
'nonce' => wp_create_nonce( 'wp_rest' ),
'endpoint' => [
'media' => esc_url_raw( rest_url( '/wp/v2/media/' ) ),
],
],
'fields' => $fields->enqueued['fields'],
'elements' => $fields->enqueued['elements'],
'dynamics' => $fields->get_dynamic_value_data(),
'mimetypes' => get_allowed_mime_types()
];

wp_add_inline_script( 'tangible-fields', 'var TangibleFields = ' . json_encode($data) . ';', 'before' );

$fields->is_enqueued = true;
};

$fields->maybe_enqueue_scripts = function() use($fields) : void {

$has_registrations = empty($fields->enqueued['fields']) && empty($fields->enqueued['elements']);

if( $has_registrations || $fields->is_enqueued ) {
return;
}

$fields->enqueue();
};

add_action( 'wp_footer', $fields->maybe_enqueue_scripts );
add_action( 'admin_footer', $fields->maybe_enqueue_scripts );
8 changes: 3 additions & 5 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php

namespace Tangible\FieldsExample;
use tangible\framework;

defined('ABSPATH') or die();

use tangible\framework;

$plugin = \tangible\create_object([
'name' => 'tangible-field-example',
'title' => 'Tangible Field Example',
Expand All @@ -15,8 +13,8 @@
'file_path' => __FILE__,
'base_path' => plugin_basename( __FILE__ ),
'dir_path' => plugin_dir_path( __FILE__ ),
'url' => plugins_url( '/', __FILE__ ),
'assets_url' => plugins_url( '/../assets', __FILE__ ),
'url' => framework\module_url( '/', __FILE__ ),
'assets_url' => framework\module_url( '/../assets', __FILE__ ),
]);

if (!function_exists('tangible_field_example')) {
Expand Down

0 comments on commit 42dc161

Please sign in to comment.