PluginProbe
Extendify / 3.2.0
Extendify v3.2.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Shared / Services / PluginDependencies / Forms / ContactForm7.php

ContactForm7.php in Extendify 3.2.0, at app/Shared/Services/PluginDependencies/Forms/ContactForm7.php

108 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Create Contact Form 7 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 Contact Form 7 programmatically.
15 */
16
17 class ContactForm7
18 {
19 /**
20 * The plugin slug.
21 *
22 * @var string
23 */
24 public static $slug = 'contact-form-7/wp-contact-form-7.php';
25
26 /**
27 * Replace the placeholder for Contact Form 7.
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:contact-form-7/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('contact-form-7', self::$slug);
45 if (is_wp_error($response)) {
46 return $response;
47 }
48 }
49
50 $formId = self::getOrCreateForm(
51 'simple-contact-form',
52 __('Simple Contact Form', 'extendify-local')
53 );
54
55 // If we didn't get the form, send back an error to retry.
56 if (!$formId) {
57 return new \WP_Error('form_error', 'Could not create form');
58 }
59
60 $hash = substr(get_post_meta($formId, '_hash', true), 0, 7);
61 // Update the id.
62 $updatedContent = preg_replace('/"id":(\d+)/', '"id":' . $formId, $newCode);
63 // Update the hash.
64 $updatedContent = preg_replace('/"hash":"[a-f0-9]+"/', '"hash":"' . $hash . '"', $updatedContent);
65 // Update the id in the shortcode (which uses the hash value).
66 $updatedContent = preg_replace(
67 '/\[contact-form-7 id="[a-f0-9]+"/',
68 '[contact-form-7 id="' . $hash . '"',
69 $updatedContent
70 );
71
72 return $updatedContent;
73 }
74
75 /**
76 * Retrieve or create a Contact Form 7
77 *
78 * @param string $template - The form template.
79 * @param string $title - The form title.
80 *
81 * @return mixed
82 */
83 private static function getOrCreateForm($template, $title)
84 {
85 $query = new \WP_Query([
86 'post_type' => 'wpcf7_contact_form',
87 'post_status' => 'publish',
88 ]);
89
90 // If we already made the same form, return the ID.
91 foreach ($query->posts as $post) {
92 $postMeta = get_post_meta($post->ID, 'extendify_form_type', true);
93
94 if ($postMeta === $template) {
95 return $post->ID;
96 }
97 }
98
99 $form = \wpcf7_save_contact_form(['title' => $title]);
100 if (!$form) {
101 return false;
102 }
103
104 add_post_meta($form->id(), 'extendify_form_type', $template);
105 return $form->id();
106 }
107 }
108