| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Create WPForm programmatically. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services\PluginDependencies\Forms; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Shared\Services\PluginDependencies\PluginInstaller; |
| 12 |
|
| 13 |
/** |
| 14 |
* Create WPForm programmatically. |
| 15 |
*/ |
| 16 |
|
| 17 |
class WPForms |
| 18 |
{ |
| 19 |
/** |
| 20 |
* The plugin slug. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public static $slug = 'wpforms-lite/wpforms.php'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Replace the placeholder for WP Forms. |
| 28 |
* |
| 29 |
* @param mixed $code - The code data. |
| 30 |
* @param string $template - The form template. |
| 31 |
* @param string $newCode - The plugin pattern code. |
| 32 |
* @return mixed |
| 33 |
*/ |
| 34 |
public static function create($code, $template, $newCode) |
| 35 |
{ |
| 36 |
// Currently we only support the simple template. |
| 37 |
if ($template !== 'simple' || !preg_match('/wp:wpforms/m', $newCode)) { |
| 38 |
return $code; |
| 39 |
} |
| 40 |
|
| 41 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 42 |
// If the plugin is already installed and active, we don't need to install it again. |
| 43 |
if (!is_plugin_active(self::$slug)) { |
| 44 |
$response = PluginInstaller::installPlugin('wpforms-lite', self::$slug); |
| 45 |
if (is_wp_error($response)) { |
| 46 |
return $response; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$wpFormSettings = get_option('wpforms_settings', []); |
| 51 |
if (!array_key_exists('disable-css', $wpFormSettings)) { |
| 52 |
$wpFormSettings['disable-css'] = 2; |
| 53 |
update_option('wpforms_settings', $wpFormSettings); |
| 54 |
} |
| 55 |
|
| 56 |
$formId = self::getOrCreateForm( |
| 57 |
'simple-contact-form-template', |
| 58 |
__('Simple Contact Form', 'extendify-local') |
| 59 |
); |
| 60 |
|
| 61 |
// If we didn't get the form, send back an error to retry. |
| 62 |
if (!$formId) { |
| 63 |
return new \WP_Error('form_error', 'Could not create wpforms form'); |
| 64 |
} |
| 65 |
|
| 66 |
// Replace {"formId": "1234"} with the actual form ID. |
| 67 |
return preg_replace('/("formId":)(\s*)"\d+"/', '${1}"' . $formId . '"', $newCode); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Retrieve or create a WPForms |
| 73 |
* |
| 74 |
* @param string $template - The form template. |
| 75 |
* @param string $title - The form title. |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public static function getOrCreateForm($template, $title) |
| 79 |
{ |
| 80 |
if (!function_exists('wpforms') || !\wpforms()->get('form')) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// Get all the forms, which is an array of WP_Post objects. |
| 85 |
$forms = \wpforms()->get('form')->get(); |
| 86 |
// If we already made the same form, return the ID. |
| 87 |
foreach ($forms as $post) { |
| 88 |
$postMeta = get_post_meta($post->ID, 'extendify_form_type', true); |
| 89 |
|
| 90 |
if ($postMeta === $template) { |
| 91 |
return $post->ID; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$formId = \wpforms()->get('form')->add($title, [], ['template' => $template]); |
| 96 |
if (!$formId) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$formPost = \wpforms()->get('form')->get($formId); |
| 101 |
// We have to manually add the form id into the form data. |
| 102 |
$formContent = \wpforms_decode($formPost->post_content); |
| 103 |
$fields = self::getFormFields($template); |
| 104 |
$newFields = [ |
| 105 |
'id' => $formId, |
| 106 |
'fields' => $fields, |
| 107 |
]; |
| 108 |
\wpforms()->get('form')->update($formId, array_merge($formContent, $newFields)); |
| 109 |
// Keep track of the form in case we need to reuse it later. |
| 110 |
\add_post_meta($formId, 'extendify_form_type', $template); |
| 111 |
return $formId; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Retrieve or create a WPForms |
| 116 |
* |
| 117 |
* @param string $template - The form template. |
| 118 |
* @return object |
| 119 |
*/ |
| 120 |
public static function getFormFields($template) |
| 121 |
{ |
| 122 |
if ($template !== 'simple-contact-form-template') { |
| 123 |
return []; |
| 124 |
} |
| 125 |
|
| 126 |
return [ |
| 127 |
1 => [ |
| 128 |
'id' => '1', |
| 129 |
'type' => 'name', |
| 130 |
'format' => 'simple', |
| 131 |
'label' => __('Name', 'extendify-local'), |
| 132 |
'required' => '1', |
| 133 |
'size' => 'large', |
| 134 |
], |
| 135 |
2 => [ |
| 136 |
'id' => '2', |
| 137 |
'type' => 'email', |
| 138 |
'label' => __('Email', 'extendify-local'), |
| 139 |
'required' => '1', |
| 140 |
'size' => 'large', |
| 141 |
], |
| 142 |
3 => [ |
| 143 |
'id' => '3', |
| 144 |
'type' => 'text', |
| 145 |
'label' => __('Subject', 'extendify-local'), |
| 146 |
'size' => 'large', |
| 147 |
], |
| 148 |
4 => [ |
| 149 |
'id' => '4', |
| 150 |
'type' => 'textarea', |
| 151 |
'label' => __('Message', 'extendify-local'), |
| 152 |
'required' => '1', |
| 153 |
'size' => 'large', |
| 154 |
], |
| 155 |
]; |
| 156 |
} |
| 157 |
} |
| 158 |
|