namespace WPForms\Admin\Builder;
use WPForms\Helpers\CacheBase;
use WPForms\Tasks\Actions\AsyncRequestTask;
* Single template cache handler.
class TemplateSingleCache extends CacheBase {
* License data (`key` and `type`).
* Determine if the class is allowed to load.
protected function allow_load() {
$has_permissions = wpforms_current_user_can( [ 'create_forms', 'edit_forms' ] );
$allowed_requests = wpforms_is_admin_ajax() || wpforms_is_admin_page( 'builder' ) || wpforms_is_admin_page( 'templates' );
$allow = wp_doing_cron() || wpforms_doing_wp_cli() || ( $has_permissions && $allowed_requests );
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
* Whether to allow to load this class.
* @param bool $allow True or false.
return (bool) apply_filters( 'wpforms_admin_builder_templatesinglecache_allow_load', $allow );
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
* Re-initialize object with the particular template.
* @param string $template_id Template ID (hash).
* @param array $license License data.
* @return TemplateSingleCache
public function instance( $template_id, $license ) {
$this->id = $template_id;
$this->license = $license;
* @return array Settings array.
protected function setup() {
'remote_source' => $this->remote_source(),
'cache_file' => $this->get_cache_file_name(),
// This filter is documented in wpforms/src/Admin/Builder/TemplatesCache.php.
// phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.PHPDocHooks.RequiredHookDocumentation
'cache_ttl' => (int) apply_filters( 'wpforms_admin_builder_templates_cache_ttl', WEEK_IN_SECONDS ),
* Generate single template remote URL.
* @param bool $cache True if the cache arg should be appended to the URL.
private function remote_source( $cache = false ) {
if ( ! isset( $this->license['key'] ) ) {
'license' => $this->license['key'],
'https://wpforms.com/templates/api/get/' . $this->id
* @return array Cached data.
if ( ! $this->updated ) {
$this->update_usage_tracking();
* Sends a request to update the form template usage tracking database.
private function update_usage_tracking() {
$tasks = wpforms()->obj( 'tasks' );
$url = $this->remote_source( true );
$args = [ 'blocking' => false ];
$tasks->create( AsyncRequestTask::ACTION )->async()->params( $url, $args )->register();
* Get cache directory path.
protected function get_cache_dir() {
return parent::get_cache_dir() . 'templates/';
* Generate single template cache file name.
private function get_cache_file_name() {
return sanitize_key( $this->id ) . '.json';
* Prepare data to store in a local cache.
* @param array $data Raw data received by the remote request.
* @return array Prepared data for caching.
protected function prepare_cache_data( $data ): array {
empty( $data['status'] ) ||
$data['status'] !== 'success' ||
$cache_data = $data['data'];
$cache_data['data'] = empty( $cache_data['data'] ) ? [] : $cache_data['data'];
$cache_data['data']['settings'] = empty( $cache_data['data']['settings'] ) ? [] : $cache_data['data']['settings'];
$cache_data['data']['settings']['ajax_submit'] = '1';
// Strip the word "Template" from the end of the template name and form title setting.
$cache_data['name'] = preg_replace( '/\sTemplate$/', '', $cache_data['name'] );
$cache_data['data']['settings']['form_title'] = $cache_data['name'];
// Unset `From Name` field of the notification settings.
// By default, the builder will use the `blogname` option value.
unset( $cache_data['data']['settings']['notifications'][1]['sender_name'] );
* Wipe cache of an empty templates.
public function wipe_empty_templates_cache() {
$cache_dir = $this->get_cache_dir();
$files = glob( $cache_dir . '*.json' );
foreach ( $files as $filename ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$content = file_get_contents( $filename );
if ( empty( $content ) || trim( $content ) === '[]' ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink