mehrshaddarzi / wp-trait

Fast and standard development of WordPress plugins
MIT License
15 stars 5 forks source link

Add View Collection #38

Closed iniznet closed 2 years ago

iniznet commented 2 years ago

image

Taken & modified from: Generate Model manually

public function admin_notices()
{
    $text = __('This Notice is a example from your plugin', $this->plugin->textdomain);

    // First Way
    $content = $this->view()
        ->attribute('text', $text)
        ->attribute([
            'description' => __('This is the description of your notice', $this->plugin->textdomain)
        ])
        ->render('notice');
    echo $this->add_alert($content, 'info');

    // Second Way
    $content = $this->view()->render('notice', [
        'text' => $text
    ], [
        'text' => __('Actually this is the real notice example from your plugin', $this->plugin->textdomain)
    ]);
    echo $this->add_alert($content, 'info');

    // Third Way
    $content = $this->view();
    $content->text = $text;
    echo $this->add_alert($content('notice'), 'info');

    // Property Way
    echo $this->add_alert($this->view->render('notice', [
        'text' => $text . ' with property way'
    ]), 'info');
}

By default the template path: plugin-directory/templates

<!-- plugin-directory/templates/notice.php -->
<strong style="display: block;"><?= $text; ?></strong>
<?php if (isset($description)): ?>
    <small><?= $description; ?></small>
<?php endif; ?>
mehrshaddarzi commented 2 years ago

Hi @iniznet Thanks a lot for this pull request. We need View system in this Package. I have a suggestion., please creating a feature for Overriding templates via a current active theme in WordPress,

// First Check User Overriding Template in your Theme
if(!file_exists(get_template_directory().'/my-plugin-slug/alert/success.php'){

// If not Exist File, then Use Plugin templates
include $this->path.'/templates/alert/success.php';
}

Example in WooCommerce: https://woocommerce.com/document/template-structure/#how-to-edit-files

iniznet commented 2 years ago

Hi @iniznet Thanks a lot for this pull request. We need View system in this Package. I have a suggestion., please creating a feature for Overriding templates via a current active theme in WordPress,

// First Check User Overriding Template in your Theme
if(!file_exists(get_template_directory().'/my-plugin-slug/alert/success.php'){

// If not Exist File, then Use Plugin templates
include $this->path.'/templates/alert/success.php';
}

Example in WooCommerce: https://woocommerce.com/document/template-structure/#how-to-edit-files

Done! What do you think?

/** src/admin.php */
public function admin_notices()
{
    $text = __('This Notice is a example from your plugin', $this->plugin->textdomain);

    // First Way
    $content = $this->view()
        ->attribute('text', $text)
        ->attribute([
            'description' => __('This is the description of your notice', $this->plugin->textdomain)
        ])
        ->render('notice');
    echo $this->add_alert($content, 'info');

    // Second Way
    $content = $this->view()->render('notice', [
        'text' => $text
    ], [
        'text' => __('Actually this is the real notice example from your plugin', $this->plugin->textdomain)
    ]);
    echo $this->add_alert($content, 'info');

    // Third Way
    $content = $this->view();
    $content->text = $text;
    echo $this->add_alert($content('notice'), 'info');

    // Property Way
    echo $this->add_alert($this->view->render('notice', [
        'text' => $text . ' with property way'
    ]), 'info');

    // Property Way with overriden view
    echo $this->add_alert($this->view->render('notice-override', [
        'text' => $text . ' with property way'
    ]), 'info');
}
<!-- themes/generatepress/plugin-slug/templates/notice-override.php -->
<strong style="display: block;"><?= $text . ' and overriden view'; ?></strong>
<?php if (isset($description)): ?>
    <small><?= $description; ?></small>
<?php endif; ?>

image

iniznet commented 2 years ago

~I think I need some more tests before we merge this. My latest commit breaks the overriding feature due to wanting to fix the open_basedir restrictions error because when a file doesn't exist it returns / path.~

It's good to go now.

mehrshaddarzi commented 2 years ago

I think adding a new parameter in render method for example $canOverride with boolean value and default is true. that Developer allow to user for overriding custom php file or not. That is a good idea?

iniznet commented 2 years ago

I think so, it's necessary to have it so that the Developer is able to toggle their templates overriding.

iniznet commented 2 years ago
/** src/admin.php:admin_notices() */

    // Property Way with overriden view
    echo $this->add_alert($this->view->render('notice-override-off', [
        'text' => $text . ' with property way and active theme should not override this view'
    ], [], false), 'info');
<!-- themes/generatepress/plugin-slug/templates/notice-override-off.php -->
<strong style="display: block;"><?= $text . ' but actually active theme can override this view'; ?></strong>
<?php if (isset($description)): ?>
    <small><?= $description; ?></small>
<?php endif; ?>

image

mehrshaddarzi commented 2 years ago

we doNot need templates in current theme dir. please replace:

$paths = [
                get_stylesheet_directory() . '/' . $this->plugin->slug . '/templates',
                get_template_directory() . '/' . $this->plugin->slug . '/templates',
            ];

To

$paths = [
                get_stylesheet_directory() . '/' . $this->plugin->slug,
                get_template_directory() . '/' . $this->plugin->slug,
            ];
iniznet commented 2 years ago

That will break the overriding feature because I'm setting:

protected function setPath($path = '', $plugin = null)
  {
      if (!$path && $plugin) {
          $path = $plugin->path . 'templates';
      }

      $this->path = $path;
  }

It will try to look: themes/generatepress/plugin-slug/notice-override.php currently:

themes/generatepress/plugin-slug/templates/notice-override.php
plugins/plugin-slug/templates/notice-override.php
iniznet commented 2 years ago

My bad, after rechecking the woocommerce docs you're right.

mehrshaddarzi commented 2 years ago

I push this request in master branch. Please Help me for writing document View Template in Readme.md. Thanks