uploads_folder = $uploads_folder; $this->settings = $settings; $this->plugin = $plugin; $this->loader = null; $this->twig = null; $this->wp_filesystem = null; } protected function get_wp_filesystem(): WP_Filesystem_Base { if ( null === $this->wp_filesystem ) { global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); $this->wp_filesystem = $wp_filesystem; } return $this->wp_filesystem; } protected function init_twig(): void { // @phpstan-ignore-next-line $this->loader = new FilesystemLoader( $this->uploads_folder ); // @phpstan-ignore-next-line $this->twig = new Environment( $this->loader, array( // 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 get_twig(): ?Environment { if ( null === $this->twig ) { if ( false === $this->get_wp_filesystem()->is_dir( $this->uploads_folder ) ) { return null; } $this->init_twig(); } return $this->twig; } protected function print_error_message( string $unique_view_id, string $error_message ): void { printf( '
Advanced Views (%s) template:
', esc_html( $unique_view_id ), esc_html( $error_message ) ); } protected function is_templates_dir_writable(): bool { $templates_dir = $this->uploads_folder; $wp_filesystem = $this->get_wp_filesystem(); if ( false === $wp_filesystem->is_dir( $templates_dir ) ) { return false; } $test_file = $templates_dir . '/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). $is_written = false !== $wp_filesystem->put_contents( $test_file, 'test' ); if ( false === $is_written ) { return false; } $content = $wp_filesystem->get_contents( $test_file ); $is_writable = 'test' === $content; $is_removed = $wp_filesystem->delete( $test_file ); return true === $is_writable && true === $is_removed; } /** * @param array' . esc_html( print_r( $args, true ) ) . ''; } } $wp_filesystem->delete( $template_file ); } public function create_templates_dir(): void { $templates_dir = $this->uploads_folder; $wp_filesystem = $this->get_wp_filesystem(); // skip if already exists. if ( true === $wp_filesystem->is_dir( $templates_dir ) ) { return; } $is_created_dir = $wp_filesystem->mkdir( $templates_dir, 0755 ); if ( false === $is_created_dir ) { $this->get_logger()->warning( "can't create the templates directory", array( 'path' => $templates_dir, ) ); return; } $wp_filesystem->put_contents( $templates_dir . '/readme.txt', 'This directory is used by the Advanced Views plugin to store logs and temporarily store Twig templates during execution.' ); $wp_filesystem->put_contents( $templates_dir . '/index.php', 'put_contents( $templates_dir . '/.htaccess', "Order Deny,Allow\nDeny from all\n" ); } public function remove_templates_dir(): void { // do not remove if switching versions. // Because activation hooks won't be called, so dir will be missing. if ( true === $this->plugin->is_switching_versions() ) { return; } $wp_filesystem = $this->get_wp_filesystem(); $templates_dir = $this->uploads_folder; if ( false === $wp_filesystem->is_dir( $templates_dir ) ) { return; } // remove the dir. $wp_filesystem->rmdir( $templates_dir, true ); } public function show_templates_dir_is_not_writable_warning(): void { $screen = get_current_screen(); // show only on the list pages of Views & Cards. if ( null === $screen || ! in_array( $screen->post_type, array( Views_Cpt::NAME, Cards_Cpt::NAME ), true ) || 'edit' !== $screen->base ) { return; } if ( true === $this->is_templates_dir_writable() ) { return; } echo '
';
echo esc_html( __( 'The templates directory is not writable.', 'acf-views' ) );
echo ' (path = ' . esc_html( $this->uploads_folder ) . ')
';
echo esc_html( __( 'Most likely, the WordPress uploads directory is not writable.', 'acf-views' ) ) . '
';
echo esc_html(
__(
'Check and fix file permissions, then deactivate and activate back the Advanced Views plugin. If the issue persists, contact support.',
'acf-views'
)
);
echo '