settings = $settings; $this->plugin = $plugin; $this->loader = null; $this->twig = null; } protected function initTwig(): void { // @phpstan-ignore-next-line $this->loader = new FilesystemLoader($this->getTemplatesDir()); // @phpstan-ignore-next-line $this->twig = new Environment($this->loader, [ // will generate exception if a var doesn't exist instead of replace to NULL 'strict_variables' => true, // 'html' by default, just highlight that it's secure to not escape TWIG variable values in PHP 'autoescape' => 'html', ]); } // @phpstan-ignore-next-line protected function getTwig(): ?Environment { if (null === $this->twig) { if (!is_dir($this->getTemplatesDir())) { return null; } $this->initTwig(); } return $this->twig; } protected function getErrorMessage(string $uniqueViewId, string $errorMessage): string { return sprintf( '
Advanced Views (%s) template:
', esc_html($uniqueViewId), esc_html($errorMessage) ); } protected function getTemplatesDir(): string { // there is no sense to make difference for basic & pro versions return wp_upload_dir()['basedir'] . '/acf-views'; } protected function isTemplatesDirWritable(): bool { $templatesDir = $this->getTemplatesDir(); if (false === is_dir($templatesDir)) { return false; } $testFile = $templatesDir . '/test.txt'; // the best way to check is to make test write // (check of permissions or 'is_writable' is not enough, as it can be set to 777, but the folder can be owned by another user) $isWrote = false !== file_put_contents($testFile, 'test'); if (false === $isWrote) { return false; } $content = file_get_contents($testFile); $isWritable = 'test' === $content; $isRemoved = unlink($testFile); return true === $isWritable && true === $isRemoved; } /** * @param array' . print_r($args, true) . ''; } } unlink($templateFile); return $html; } public function createTemplatesDir(): void { $templatesDir = $this->getTemplatesDir(); // skip if already exists if (is_dir($templatesDir)) { return; } $isCreatedDir = mkdir($templatesDir, 0755); if (!$isCreatedDir) { return; } file_put_contents( $templatesDir . '/readme.txt', 'This directory is used by the Advanced Views plugin to temporarily store Twig templates during execution.' ); file_put_contents($templatesDir . '/index.php', 'plugin->isSwitchingVersions()) { return; } $templatesDir = $this->getTemplatesDir(); if (!is_dir($templatesDir)) { return; } $templates = glob("$templatesDir/*.*"); if (false !== $templates) { // remove readme.txt & index.php array_map('unlink', $templates); } // remove the dir rmdir($templatesDir); } public function showTemplatesDirIsNotWritableWarning(): void { $screen = get_current_screen(); // show only on the list pages of Views & Cards if (null === $screen || !in_array($screen->post_type, [ViewsCpt::NAME, CardsCpt::NAME,], true) || 'edit' !== $screen->base) { return; } if (true === $this->isTemplatesDirWritable()) { return; } $message = __('The templates directory is not writable.', 'acf-views'); $message .= ' (path = ' . $this->getTemplatesDir() . ')
%s