get('form')) { return false; } // Get all the forms, which is an array of WP_Post objects. $forms = \wpforms()->get('form')->get(); // If we already made the same form, return the ID. foreach ($forms as $post) { $postMeta = get_post_meta($post->ID, 'extendify_form_type', true); if ($postMeta === $template) { return $post->ID; } } $formId = \wpforms()->get('form')->add($title, [], ['template' => $template]); if (!$formId) { return false; } $formPost = \wpforms()->get('form')->get($formId); // We have to manually add the form id into the form data. $formContent = \wpforms_decode($formPost->post_content); $fields = self::getFormFields($template); $newFields = [ 'id' => $formId, 'fields' => $fields, ]; \wpforms()->get('form')->update($formId, array_merge($formContent, $newFields)); // Keep track of the form in case we need to reuse it later. \add_post_meta($formId, 'extendify_form_type', $template); return $formId; } /** * Retrieve or create a WPForms * * @param string $template - The form template. * @return object */ public static function getFormFields($template) { if ($template !== 'simple-contact-form-template') { return []; } return [ 1 => [ 'id' => '1', 'type' => 'name', 'format' => 'simple', 'label' => __('Name', 'extendify-local'), 'required' => '1', 'size' => 'large', ], 2 => [ 'id' => '2', 'type' => 'email', 'label' => __('Email', 'extendify-local'), 'required' => '1', 'size' => 'large', ], 3 => [ 'id' => '3', 'type' => 'text', 'label' => __('Subject', 'extendify-local'), 'size' => 'large', ], 4 => [ 'id' => '4', 'type' => 'textarea', 'label' => __('Message', 'extendify-local'), 'required' => '1', 'size' => 'large', ], ]; } }