Form.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Form_Submissions; |
| 4 | |
| 5 | use IAWP\Illuminate_Builder; |
| 6 | use IAWP\Query; |
| 7 | /** |
| 8 | * Form tracking requires dynamic columns. If you have 2 forms, there will be 6 new columns. 2 for |
| 9 | * a summary and 2 per form. This class makes it easy to get all the forms a given site has, while |
| 10 | * caching the result, so it doesn't need to re-fetch from the database. |
| 11 | * @internal |
| 12 | */ |
| 13 | class Form |
| 14 | { |
| 15 | private $id; |
| 16 | private $title; |
| 17 | private $plugin_id; |
| 18 | private static $forms = null; |
| 19 | 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']]]; |
| 20 | private function __construct(int $id, string $title, int $plugin_id) |
| 21 | { |
| 22 | $this->id = $id; |
| 23 | $this->title = $title; |
| 24 | $this->plugin_id = $plugin_id; |
| 25 | } |
| 26 | public function id() : int |
| 27 | { |
| 28 | return $this->id; |
| 29 | } |
| 30 | public function title() : string |
| 31 | { |
| 32 | return $this->title; |
| 33 | } |
| 34 | public function icon() : string |
| 35 | { |
| 36 | $lowercase = \strtolower($this->plugin_name()); |
| 37 | $hyphenated = \str_replace(' ', '_', $lowercase); |
| 38 | return $hyphenated; |
| 39 | } |
| 40 | public function plugin_name() : string |
| 41 | { |
| 42 | return \IAWP\Form_Submissions\Form::find_plugin_by_id($this->plugin_id)['name']; |
| 43 | } |
| 44 | public function is_plugin_active() : bool |
| 45 | { |
| 46 | return self::static_is_plugin_active($this->plugin_id); |
| 47 | } |
| 48 | public function submissions_column() : string |
| 49 | { |
| 50 | return "form_submissions_for_{$this->id}"; |
| 51 | } |
| 52 | public function conversion_rate_column() : string |
| 53 | { |
| 54 | return "form_conversion_rate_for_{$this->id}"; |
| 55 | } |
| 56 | public static function has_active_form_plugin() : bool |
| 57 | { |
| 58 | return \is_array(self::get_active_plugin()); |
| 59 | } |
| 60 | public static function get_active_form_plugin_name() : ?string |
| 61 | { |
| 62 | $active_plugin = self::get_active_plugin(); |
| 63 | return \is_array($active_plugin) ? $active_plugin['name'] : null; |
| 64 | } |
| 65 | public static function find_form_by_column_name(string $column_name) : ?\IAWP\Form_Submissions\Form |
| 66 | { |
| 67 | $id = \intval(\preg_match('/(\\d+)\\z/', $column_name)); |
| 68 | $forms = self::get_forms(); |
| 69 | foreach ($forms as $form) { |
| 70 | if ($id === $form->id()) { |
| 71 | return $form; |
| 72 | } |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 | /** |
| 77 | * @return Form[] |
| 78 | */ |
| 79 | public static function get_forms() : array |
| 80 | { |
| 81 | if (\is_array(self::$forms)) { |
| 82 | return self::$forms; |
| 83 | } |
| 84 | $forms_table = Query::get_table_name(Query::FORMS); |
| 85 | $query = Illuminate_Builder::get_builder()->select(['form_id', 'cached_form_title', 'plugin_id'])->from($forms_table); |
| 86 | $forms = \array_filter(\array_map(function ($form) { |
| 87 | if (\is_null(self::find_plugin_by_id($form->plugin_id))) { |
| 88 | return null; |
| 89 | } |
| 90 | return new self($form->form_id, $form->cached_form_title, $form->plugin_id); |
| 91 | }, $query->get()->all())); |
| 92 | \usort($forms, function (\IAWP\Form_Submissions\Form $a, \IAWP\Form_Submissions\Form $b) { |
| 93 | // First level sort by plugin name |
| 94 | $plugin_name_comparison = \strcmp($a->plugin_name(), $b->plugin_name()); |
| 95 | // If types are equal, sort by 'name' |
| 96 | if ($plugin_name_comparison === 0) { |
| 97 | return \strcmp($a->title(), $b->title()); |
| 98 | } |
| 99 | return $plugin_name_comparison; |
| 100 | }); |
| 101 | self::$forms = $forms; |
| 102 | return self::$forms; |
| 103 | } |
| 104 | private static function get_active_plugin() : ?array |
| 105 | { |
| 106 | foreach (\IAWP\Form_Submissions\Form::$plugins as $plugin) { |
| 107 | if (self::static_is_plugin_active($plugin['id'])) { |
| 108 | return $plugin; |
| 109 | } |
| 110 | } |
| 111 | return null; |
| 112 | } |
| 113 | private static function static_is_plugin_active(int $plugin_id) : bool |
| 114 | { |
| 115 | $active_plugins = \get_option('active_plugins'); |
| 116 | $plugin = \IAWP\Form_Submissions\Form::find_plugin_by_id($plugin_id); |
| 117 | if (\array_key_exists('theme', $plugin)) { |
| 118 | if (\get_template() === $plugin['theme']) { |
| 119 | return \true; |
| 120 | } |
| 121 | } |
| 122 | if (\array_key_exists('plugin_slugs', $plugin)) { |
| 123 | foreach ($plugin['plugin_slugs'] as $slug) { |
| 124 | if (\in_array($slug, $active_plugins)) { |
| 125 | return \true; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return \false; |
| 130 | } |
| 131 | private static function find_plugin_by_id(int $id) : ?array |
| 132 | { |
| 133 | foreach (self::$plugins as $plugin) { |
| 134 | if ($plugin['id'] === $id) { |
| 135 | return $plugin; |
| 136 | } |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 |