Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Twig template can not be found if open_basedir is set and template is provided as full path #2891

Closed
marcoluzi opened this issue Jan 25, 2024 · 9 comments
Labels
2.0 bug needs-info Need more info on this issue in order to properly investigate

Comments

@marcoluzi
Copy link

Expected Behavior

When the Twig Package tries to find the templates, it merges $path and $shortname together to look for the template. See here. When I call the Timber::compile(), the $filenames parameter is passed as a full path string and open_basedir is not "" the full path passed in $filenames should be adjusted accordingly.

Actual behavior

When open_basedir is set and the desired Template is passed as a full path string, the Twig Package will look for the file in a faulty location. For example, if open_basedir is set, one of the paths in the $paths variable is "/var/www/project-name.com". Twig will add the template name behind it, which will result in a file path like this: /var/www/project-name.com//var/www/project-name.com/wp-content/themes/theme-xyz/blocks/block-xy/markup.twig.

Steps to reproduce behavior

  1. Setup a location, where open_basedir is set.
  2. Try to render a Template using an absolute path instead of one of the template names stored in one of the Timber::$dirname locations.

Notes

No response

What version of Timber are you using?

2.0.0

What version of WordPress are you using?

6.4.2

What version of PHP are you using?

No response

How did you install Timber?

Installed or updated Timber through Composer

@Levdbas
Copy link
Member

Levdbas commented Jan 26, 2024

Hi @marcoluzi , can you tell me a little bit about your use case? Why are you setting open_basedir in the first place?

@marcoluzi
Copy link
Author

@Levdbas open_basedir is used on the server the website is running on. I don't know much about that server setup and can not change it. I am using absolute path, because I built a dynamic Gutenberg Block logic, that uses Twig to render the frontend view. Each Block has a dedicated markup.twig, which will be be used while looping through all the block directories.

@marcoluzi
Copy link
Author

If anyone has the same problem, this is a quick fix I use to manipulate the absolute template path before i pass it to Timber::compile() method:

$open_basedir = ini_get('open_basedir');

if ($open_basedir) {
	$twig_markup_file_path = str_replace(ABSPATH, '', $twig_markup_file_path);
}

@Levdbas
Copy link
Member

Levdbas commented Feb 23, 2024

Hi @marcoluzi , could you provide a full example from your setup of $twig_markup_file_path to the Timber::render call? I will try to work on a fix for this then.

@marcoluzi
Copy link
Author

Hey @Levdbas
This is the method I use to register the gutenberg blocks. It is hooked to the init hook:

	public function registerThemeBlocks()
	{
		if (file_exists(THEME_DIR.'/dist/blocks')) {
			// Find all block.json in blocks folder
			$block_json_files = glob(THEME_DIR.'/dist/blocks/*/block.json');

			foreach ($block_json_files as $filename) {
				// Get block folder and name
				$block_folder = dirname($filename);
				$block_name = basename($block_folder);

				// Parse json to get support options
				$block_json = json_decode(file_get_contents($filename), true);
				$block_options = [
					'supports' => $block_json['supports'] ?? [],
				];

				// Add render callback if markup.twig exists
				$twig_markup_file_path = $block_folder.'/markup.twig';

				$open_basedir = ini_get('open_basedir');

				if ($open_basedir) {
					$twig_markup_file_path = str_replace(ABSPATH, '', $twig_markup_file_path);
				}

				if (file_exists($twig_markup_file_path)) {
					$block_options['render_callback'] = function ($attributes, $content, $block) use ($twig_markup_file_path, $block_name) {
						// Set context for the block view
						$context = Timber::context();
						$context['attributes'] = $attributes;
						$context['content'] = $content;
						$context['block'] = $block;

						// Add anchor to block if set
						if (isset($attributes['anchor'])) {
							$context['anchor'] = sprintf('id="%s"', $attributes['anchor']);
						}

						// Add classes to block, use extraClasses attribute to set additional classes
						$classes = [
							'defaultClass' => $block_name.' wp-block-'.$block_name,
							'blockClasses' => $attributes['className'] ?? null,
							'extraClasses' => $attributes['extraClasses'] ?? null,
							'alignClasses' => isset($attributes['align']) ? 'align'.$attributes['align'] : null,
						];

						// Generate classes string
						$context['classes'] = sprintf('class="%s"', implode(' ', array_filter($classes)));

						return Timber::compile($twig_markup_file_path, $context);
					};
				}

				register_block_type_from_metadata($block_folder, $block_options);
			}
		}
	}

The blocks themselfe are built like they are in the 10up-theme, only I am utilizing a markup.twig instead of a markup.php.

If you need anything else, hit me up.

@marcoluzi
Copy link
Author

Hey @Levdbas

I hope you are doing well. Could I have an update on this issue?

@Levdbas
Copy link
Member

Levdbas commented May 7, 2024

Hi @marcoluzi , I tried to reproduce your error but for some reason I can't. I set open basedir in the wp-config.php file

ini_set('open_basedir', '/home/erik/sites/test-site');

Made a test block as well with a shortened version of your code.

$block_json_files = glob(get_stylesheet_directory() . '/blocks/test-block/block.json');

foreach ($block_json_files as $filename) {
	// Get block folder and name
	$block_folder = dirname($filename);
	$block_name = basename($block_folder);
	Timber::render($block_folder . '/' . $block_name . '.twig');
}

But it just loads. So not sure what would cause your problem. If you know, feel free to do a PR.

@Levdbas Levdbas added the needs-info Need more info on this issue in order to properly investigate label May 7, 2024
@marcoluzi
Copy link
Author

Hey @Levdbas,

Thank you for the testing. I just tested it again on the server where the error was occurring, and it doesn't happen anymore. So, there were probably some server-side package updates that fixed the issue. I guess we can close the issue for now. Sorry for the inconvenience.

@Levdbas
Copy link
Member

Levdbas commented May 23, 2024

Hi @marcoluzi , thank you for your feedback on this matter. Glad the problem is resolved now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 bug needs-info Need more info on this issue in order to properly investigate
Projects
None yet
Development

No branches or pull requests

3 participants