Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
413 stars 56 forks source link

Proposal: AcfComposer - relative path calculation #125

Closed davideprevosto closed 1 year ago

davideprevosto commented 2 years ago

Hello Brandon,

before adding a new PR, I would like to ask if it is worth it for you.

I am adding blocks using a reusable custom plugin each. For example, this is a simple block for adding a custom Call to Action:

# my-plugin.php
// Initialise plugin
add_filter('after_setup_theme', function () {
    if (!function_exists('app')) {
        return;
    }

    // Register plugin to Acorn
    app()->register(
        Encodia\BlockCta\Providers\PluginServiceProvider::class
    );
}, 20);
# PluginServiceProvider.php
public function boot(): void
{
    $this->app->make('AcfComposer')->registerPlugin(
         plugin_dir_path(__DIR__),
         __NAMESPACE__
     );

     // Registra manualmente il Component
     Blade::component('cta', Cta::class);
}

I am able to use it only if I change one line into the AcfComposer.php class, line 91. In this way both implementations should work: by a plugin or by a classic block into the theme.

From:

$relativePath = str_replace(
     $this->app->path() . DIRECTORY_SEPARATOR,
      '',
      $file->getPathname()
);

To:

$relativePath = str_replace(
    rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
    '',
    $file->getPathname()
);

I'm not sure if I was clear enough, I hope so. Do you think I could send you a PR or maybe I'm making a mistake in initializing our plugins?

Thank you

Log1x commented 1 year ago

Maybe Str::finish($this->app->path(), DIRECTORY_SEPARATOR) would work?

Feel free to do a PR and I'll try to test it out. As long as it doesn't break anything with normal registration and doesn't break having blocks inside of sub directories.

Log1x commented 1 year ago

Had a chance to try this. It looks like Str::finish($path, DIRECTORY_SEPARATOR) does the trick. Want to do a PR?

davideprevosto commented 1 year ago

Yes, please allow me few time and we will send to you a PR. Thank you!