Form.php
6 months ago
Newsletter.php
6 months ago
Submission.php
5 months ago
Submission_Listener.php
6 months ago
Form.php
174 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Form_Submissions; |
| 4 | |
| 5 | use IAWP\Illuminate_Builder; |
| 6 | use IAWP\Query; |
| 7 | use IAWP\Utils\Plugin; |
| 8 | use IAWPSCOPED\Illuminate\Support\Str; |
| 9 | /** |
| 10 | * Form tracking requires dynamic columns. If you have 2 forms, there will be 6 new columns. 2 for |
| 11 | * a summary and 2 per form. This class makes it easy to get all the forms a given site has, while |
| 12 | * caching the result, so it doesn't need to re-fetch from the database. |
| 13 | * @internal |
| 14 | */ |
| 15 | class Form |
| 16 | { |
| 17 | private $id; |
| 18 | private $title; |
| 19 | private $plugin_id; |
| 20 | private static $forms = null; |
| 21 | private static $is_plugin_active_cache = []; |
| 22 | private static $plugins = [['id' => 1, 'name' => 'Fluent Forms', 'plugin_slugs' => ['fluentform/fluentform.php']], ['id' => 2, 'name' => 'WPForms', 'plugin_slugs' => ['wpforms-lite/wpforms.php', 'wpforms/wpforms.php']], ['id' => 3, 'name' => 'Contact Form 7', 'plugin_slugs' => ['contact-form-7/wp-contact-form-7.php']], ['id' => 4, 'name' => 'Gravity Forms', 'plugin_slugs' => ['gravityforms/gravityforms.php']], ['id' => 5, 'name' => 'Ninja Forms', 'plugin_slugs' => ['ninja-forms/ninja-forms.php']], ['id' => 6, 'name' => 'MailOptin', 'plugin_slugs' => ['mailoptin/mailoptin.php']], ['id' => 7, 'name' => 'Convert Pro', 'plugin_slugs' => ['convertpro/convertpro.php']], ['id' => 8, 'name' => 'Elementor Pro', 'plugin_slugs' => ['elementor-pro/elementor-pro.php']], ['id' => 9, 'name' => 'JetFormBuilder', 'plugin_slugs' => ['jetformbuilder/jet-form-builder.php']], ['id' => 10, 'name' => 'Formidable Forms', 'plugin_slugs' => ['formidable/formidable.php']], ['id' => 11, 'name' => 'WS Form', 'plugin_slugs' => ['ws-form/ws-form.php', 'ws-form-pro/ws-form.php']], ['id' => 12, 'name' => 'Amelia', 'plugin_slugs' => ['ameliabooking/ameliabooking.php']], ['id' => 13, 'name' => 'Bricks Builder', 'theme' => 'bricks'], ['id' => 14, 'name' => 'ARForms', 'plugin_slugs' => ['arforms-form-builder/arforms-form-builder.php']], ['id' => 15, 'name' => 'Custom form submissions'], ['id' => 16, 'name' => 'Bit Form', 'plugin_slugs' => ['bit-form/bitforms.php']], ['id' => 17, 'name' => 'Forminator', 'plugin_slugs' => ['forminator/forminator.php']], ['id' => 18, 'name' => 'Hustle', 'plugin_slugs' => ['wordpress-popup/popover.php', 'hustle/opt-in.php']], ['id' => 19, 'name' => 'Avada', 'plugin_slugs' => ['fusion-builder/fusion-builder.php', 'fusion-core/fusion-core.php']], ['id' => 20, 'name' => 'WP Store Locator', 'plugin_slugs' => ['wp-store-locator/wp-store-locator.php']], ['id' => 21, 'name' => 'Thrive Leads', 'plugin_slugs' => ['thrive-leads/thrive-leads.php']], ['id' => 22, 'name' => 'SureForms', 'plugin_slugs' => ['sureforms/sureforms.php']], ['id' => 23, 'name' => 'Kali Forms', 'plugin_slugs' => ['kali-forms/kali-forms.php']], ['id' => 24, 'name' => 'Divi', 'theme' => 'Divi'], ['id' => 25, 'name' => 'MailPoet', 'plugin_slugs' => ['mailpoet/mailpoet.php']], ['id' => 26, 'name' => 'Mailchimp', 'plugin_slugs' => ['mailchimp-for-wp/mailchimp-for-wp.php']], ['id' => 27, 'name' => 'Kadence', 'plugin_slugs' => ['kadence-blocks/kadence-blocks.php']], ['id' => 29, 'name' => 'Newsletter', 'plugin_slugs' => ['newsletter/plugin.php']], ['id' => 30, 'name' => 'Everest Forms', 'plugin_slugs' => ['everest-forms/everest-forms.php']]]; |
| 23 | /** |
| 24 | * @var array A key/value pair (plugin_id/bool) of plugin IDs |
| 25 | */ |
| 26 | private static $has_any_tracked_submissions_cache = []; |
| 27 | private function __construct(int $id, string $title, int $plugin_id) |
| 28 | { |
| 29 | $this->id = $id; |
| 30 | $this->title = $title; |
| 31 | $this->plugin_id = $plugin_id; |
| 32 | } |
| 33 | public function id() : int |
| 34 | { |
| 35 | return $this->id; |
| 36 | } |
| 37 | public function title() : string |
| 38 | { |
| 39 | return $this->title; |
| 40 | } |
| 41 | public function icon() : string |
| 42 | { |
| 43 | $lowercase = \strtolower($this->plugin_name()); |
| 44 | $hyphenated = \str_replace(' ', '_', $lowercase); |
| 45 | return $hyphenated; |
| 46 | } |
| 47 | public function plugin_name() : string |
| 48 | { |
| 49 | return \IAWP\Form_Submissions\Form::find_plugin_by_id($this->plugin_id)['name']; |
| 50 | } |
| 51 | public function is_plugin_active() : bool |
| 52 | { |
| 53 | return self::static_is_plugin_active($this->plugin_id); |
| 54 | } |
| 55 | public function submissions_column() : string |
| 56 | { |
| 57 | return "form_submissions_for_{$this->id}"; |
| 58 | } |
| 59 | public function conversion_rate_column() : string |
| 60 | { |
| 61 | return "form_conversion_rate_for_{$this->id}"; |
| 62 | } |
| 63 | public static function has_active_form_plugin() : bool |
| 64 | { |
| 65 | return \is_array(self::get_first_active_form_plugin()); |
| 66 | } |
| 67 | public static function get_first_active_form_plugin_name() : ?string |
| 68 | { |
| 69 | $active_plugin = self::get_first_active_form_plugin(); |
| 70 | return \is_array($active_plugin) ? $active_plugin['name'] : null; |
| 71 | } |
| 72 | public static function find_form_by_column_name(string $column_name) : ?\IAWP\Form_Submissions\Form |
| 73 | { |
| 74 | $id = Str::match('/\\d+\\z/', $column_name); |
| 75 | if (!\ctype_digit($id)) { |
| 76 | return null; |
| 77 | } |
| 78 | $id = \intval($id); |
| 79 | $forms = self::get_forms(); |
| 80 | foreach ($forms as $form) { |
| 81 | if ($id === $form->id()) { |
| 82 | return $form; |
| 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | /** |
| 88 | * @return Form[] |
| 89 | */ |
| 90 | public static function get_forms() : array |
| 91 | { |
| 92 | if (\is_array(self::$forms)) { |
| 93 | return self::$forms; |
| 94 | } |
| 95 | $forms_table = Query::get_table_name(Query::FORMS); |
| 96 | $query = Illuminate_Builder::new()->select(['form_id', 'cached_form_title', 'plugin_id'])->from($forms_table); |
| 97 | $forms = \array_filter(\array_map(function ($form) { |
| 98 | if (\is_null(self::find_plugin_by_id($form->plugin_id))) { |
| 99 | return null; |
| 100 | } |
| 101 | return new self($form->form_id, $form->cached_form_title, $form->plugin_id); |
| 102 | }, $query->get()->all())); |
| 103 | \usort($forms, function (\IAWP\Form_Submissions\Form $a, \IAWP\Form_Submissions\Form $b) { |
| 104 | // First level sort by plugin name |
| 105 | $plugin_name_comparison = \strcmp($a->plugin_name(), $b->plugin_name()); |
| 106 | // If types are equal, sort by 'name' |
| 107 | if ($plugin_name_comparison === 0) { |
| 108 | return \strcmp($a->title(), $b->title()); |
| 109 | } |
| 110 | return $plugin_name_comparison; |
| 111 | }); |
| 112 | self::$forms = $forms; |
| 113 | return self::$forms; |
| 114 | } |
| 115 | public static function find_plugin_by_id(int $id) : ?array |
| 116 | { |
| 117 | foreach (self::$plugins as $plugin) { |
| 118 | if ($plugin['id'] === $id) { |
| 119 | return $plugin; |
| 120 | } |
| 121 | } |
| 122 | return null; |
| 123 | } |
| 124 | private static function has_any_tracked_submissions(int $plugin_id) : bool |
| 125 | { |
| 126 | if (\array_key_exists($plugin_id, self::$has_any_tracked_submissions_cache)) { |
| 127 | return self::$has_any_tracked_submissions_cache[$plugin_id]; |
| 128 | } |
| 129 | $forms_table = Query::get_table_name(Query::FORMS); |
| 130 | $form_submissions_table = Query::get_table_name(Query::FORM_SUBMISSIONS); |
| 131 | $has_submissions = Illuminate_Builder::new()->from($forms_table, 'forms')->join("{$form_submissions_table} AS form_submissions", 'forms.form_id', '=', 'form_submissions.form_id')->where('forms.plugin_id', '=', $plugin_id)->exists(); |
| 132 | self::$has_any_tracked_submissions_cache[$plugin_id] = $has_submissions; |
| 133 | return $has_submissions; |
| 134 | } |
| 135 | private static function get_first_active_form_plugin() : ?array |
| 136 | { |
| 137 | foreach (\IAWP\Form_Submissions\Form::$plugins as $plugin) { |
| 138 | if (self::static_is_plugin_active($plugin['id'])) { |
| 139 | return $plugin; |
| 140 | } |
| 141 | } |
| 142 | return null; |
| 143 | } |
| 144 | private static function static_is_plugin_active(int $plugin_id) : bool |
| 145 | { |
| 146 | if (\array_key_exists($plugin_id, self::$is_plugin_active_cache)) { |
| 147 | return self::$is_plugin_active_cache[$plugin_id]; |
| 148 | } |
| 149 | $plugin = \IAWP\Form_Submissions\Form::find_plugin_by_id($plugin_id); |
| 150 | if (!\array_key_exists('plugin_slugs', $plugin) && !\array_key_exists('theme', $plugin)) { |
| 151 | if (self::has_any_tracked_submissions($plugin_id)) { |
| 152 | self::$is_plugin_active_cache[$plugin_id] = \true; |
| 153 | return \true; |
| 154 | } |
| 155 | } |
| 156 | if (\array_key_exists('theme', $plugin)) { |
| 157 | if (\strtolower(\get_template()) === \strtolower($plugin['theme'])) { |
| 158 | self::$is_plugin_active_cache[$plugin_id] = \true; |
| 159 | return \true; |
| 160 | } |
| 161 | } |
| 162 | if (\array_key_exists('plugin_slugs', $plugin)) { |
| 163 | foreach ($plugin['plugin_slugs'] as $slug) { |
| 164 | if (\is_plugin_active($slug)) { |
| 165 | self::$is_plugin_active_cache[$plugin_id] = \true; |
| 166 | return \true; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | self::$is_plugin_active_cache[$plugin_id] = \false; |
| 171 | return \false; |
| 172 | } |
| 173 | } |
| 174 |