roots / sage-installer

Sage 9 installer
https://github.com/roots/sage
MIT License
30 stars 90 forks source link

Add option to move required theme files to theme root folder #3

Open QWp6t opened 7 years ago

QWp6t commented 7 years ago

See: https://github.com/roots/sage/issues/1938

jkwasniak commented 7 years ago

It would be great if a task to package theme (of using the WordPress installer) with all required dependencies is added so it can be directly invoked using yarn run (example: yarn run build:wordpress-installer-production).

dmgawel commented 6 years ago

I just made an attempt (successful for now) to move required files to root directory. I'd like to share steps to reproduce this modified setup and issues I've come across. Maybe this will help with development.

Sage setup with required theme files in top-level directory

  1. Move index.php, functions.php, style.css and screenshot.png to theme's root directory.

  2. Move resources/views to root (/views). This is required for Page Templates to work because WordPress is looking for them only one level deeper than style.css is located.

Then, tell Sage where to look for view files. In config/views.php:

     'paths' => [
-        get_theme_file_path().'/resources/views',
-        get_parent_theme_file_path().'/resources/views',
+        get_theme_file_path().'/views',
+        get_parent_theme_file_path().'/views',
     ],

Additionally, until https://github.com/roots/sage/pull/1980 is merged, fix template hierarchy in app/helpers.php:

function filter_templates($templates)
                     return [
                         "{$path}/{$template}.blade.php",
                         "{$path}/{$template}.php",
-                        "{$template}.blade.php",
-                        "{$template}.php",
                     ];
-                });
+                })
+                ->concat([
+                    "{$template}.blade.php",
+                    "{$template}.php",
+                ]);
         })
         ->filter()
         ->unique()
  1. In functions.php fix paths to loaded files:

Composer autoloader:

/**
  * Ensure dependencies are loaded
  */
 if (!class_exists('Roots\\Sage\\Container')) {
-    if (!file_exists($composer = __DIR__.'/../vendor/autoload.php')) {
+    if (!file_exists($composer = __DIR__.'/vendor/autoload.php')) {
         $sage_error(
             __('You must run <code>composer install</code> from the Sage directory.', 'sage'),
             __('Autoloader not found.', 'sage')

Sage required files:

/**
  * Add or remove files to the array as needed. Supports child theme overrides.
  */
 array_map(function ($file) use ($sage_error) {
-    $file = "../app/{$file}.php";
+    $file = "./app/{$file}.php";
     if (!locate_template($file, true, true)) {
         $sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
     }

Disable filter correcting paths to theme in default Sage setup:

-array_map(
-    'add_filter',
-    ['theme_file_path', 'theme_file_uri', 'parent_theme_file_path', 'parent_theme_file_uri'],
-    array_fill(0, 4, 'dirname')
-);

Paths to config files:

 Container::getInstance()
     ->bindIf('config', function () {
         return new Config([
-            'assets' => require dirname(__DIR__).'/config/assets.php',
-            'theme' => require dirname(__DIR__).'/config/theme.php',
-            'view' => require dirname(__DIR__).'/config/view.php',
+            'assets' => require __DIR__.'/config/assets.php',
+            'theme' => require __DIR__.'/config/theme.php',
+            'view' => require __DIR__.'/config/view.php',
         ]);
     }, true);
  1. Fix paths to controllers and models (if used).

Default directory in soberwp/controller and soberwp/models assume that required files are in resources directory. This might change in future (https://github.com/soberwp/controller/issues/54) but for now in app/setup.php add:

/**
 * Soberwp Models
 */
add_filter('sober/models/path', function () {
    return get_theme_file_path() . '/app/models';
});

/**
 * Soberwp Controller
 */
add_filter('sober/controller/path', function () {
    return get_theme_file_path() . '/app/controllers';
});

That's it. Everything should be working now as expected. Hope this helps :-)

MikeiLL commented 6 years ago

@dmgawel Does this fix work at this point? From your EDIT note above I am gathering not.

dmgawel commented 6 years ago

@MikeiLL I've just updated my previous post. It now contains working solution :-)

MikeiLL commented 6 years ago

Hey thanks @dmgawel I'm almost there.

However, illuminate/view/FileViewFinder.php is looking for blades in

partials.page-headerArray
(
    [0] => /var/www/html/wp-content/themes/MyTheme/resources/views
    [1] => /var/www/html/wp-content/themes/MyTheme/resources/views
)

If I copy the views directory back there the theme loads, but that's obviously not the answer.

Is there config somewhere I missed? Asking at Roots Discourse as well. Thanks.

MikeiLL commented 6 years ago

Found it.

Update config/views.php.

'paths' => [
    get_theme_file_path().'/resources/views',
    get_parent_theme_file_path().'/resources/views',
],

Remove /resources from the paths.

dmgawel commented 6 years ago

@MikeiLL thanks! Indeed, I have this change also in my code but forgot to include it. I'm going to edit my post and add it for future reference.

josialoos commented 2 years ago

Everyone who is interested in uploading roots-built sage theme to a normal wp-install on shared hosting ftp server setups should have a look at this script by @knowler. It's super neat tool for this. as easy as using one yarn command, uploading via ftp and activating the theme. voila. https://github.com/knowler/sage-prep