libreform / wp-libre-form

Easy native HTML5 forms for WordPress. Version 1.5 is unmaintained, but works without issue. 2.0 has been rewritten from the ground, and can be found at https://github.com/libreform/libreform
https://wordpress.org/plugins/wp-libre-form
GNU General Public License v3.0
67 stars 27 forks source link

Create a PHP API for querying the list of the form's fields #88

Closed aoloe closed 5 years ago

aoloe commented 6 years ago

A libre form plugin should be able to query the list of the form's fields.

In the code I could not find any way to do that and the documentation does not mention it...

k1sul1 commented 6 years ago

There isn't one (and there probably won't be one, thoughts @libreform/maintainers?), because it's just HTML.

You can get the fields from the post meta. https://github.com/libreform/wp-libre-form/pull/71/commits/47c149ff2e114212a34d68d74870dfa2d8c8ce1d#diff-0d4891e9fc1085e3de1a2ff4a0885cc0R75

When the form builder I'm working on is in working condition, it will transform the form HTML into JSON, that just may be generic enough to be useful, but there's no ETAs on it.

I don't know what you are doing, but you can store fields in an array, and render that array into HTML, if you want to be able to get a chunk of HTML for a type of field, but that's DIY.

timiwahalahti commented 6 years ago

Again, I agree with @k1sul1 here. Because form is pure and simple HTML, I don't see why somebody would need endpoint for getting fields that they made themself. Of course form builder will change this slightly, but as said there will be simple JSON data available about the fields.

Maybe you could provide us some details why you see this necessary and what you are working with? It would help to understand why are you needing API for getting fields.

aoloe commented 6 years ago

i'm trying to get the export plugin to work for my form.

when exporting a specific form i get the error:

Row item count was different from header item count.

i had to dig into the code to try to understand the reasons for the failure and currently i'm supposing that it does not like when fields are left empty.

as you're suggesting here, the plugin's author is using the post meta to find out the list of the fields and filter out the ones prefixed with a '_'. but there seems to be a referrer field which is not prefixed.

i guess that the _ means private or hidden for you, so it should not leak outside of the module creating it.

that's why i'd like libreform to be able to read the form properties and provide the plugin with information like the list of the fields in the form. in the form of a nice and documented (php) API call.

k1sul1 commented 6 years ago

The full error message is here: https://github.com/libreform/export/blob/master/wplf-export.php#L193-L197

The error should only happen when you're exporting submissions from different forms, or when you've added fields to the form after some submissions came came in, but the file should still be downloadable, if you just have the correct link.

https://github.com/libreform/export/blob/master/wplf-export.php#L59-L78 Unless array_filter also filters out falsy values when used with a callback, it certainly shouldn't care about empty field values, and I must've tested that at some point.

_ denotes a hidden field in the post meta, not meant for human eyes. Someone might add ACF fields into their submissions, and they certainly do not want to have the internal field keys polluting their export.

k1sul1 commented 6 years ago

I did a test export with the default form, leaving name empty on second submission. Works as intended. The referrer is usually the most important info, it should be in the export.

If you don't like what's exported, you can customize the filter function to leave the referrer out, or allow all fields to be exported.

name,email,message,referrer
,test@test,nope,https://wp.local/libre-forms/test-form/
Test,test@test,test,https://wp.local/libre-forms/test-form/
aoloe commented 6 years ago

sorry, i think really don't understand the mindset of this project's developers.

every software i have ever used, exports exactly what i have defined. now you tell me that the most import field exported by your plugin is a field that i was not aware it even existed!

anyway, the most important point: when working on a plugin, i should not have to care about the core private fields. and i really think that it's the core's task to provide me a "clean" list of the form's field.

k1sul1 commented 6 years ago

I don't think that you understand WordPress :)

And I just told you that you can export exactly what you have defined, by using the filter function. This is an example what might be saved in the database for a submission.

MariaDB [vagrant]> select * from wp_postmeta WHERE post_id = 3438;
+---------+---------+---------------------+--------------------------------------------+
| meta_id | post_id | meta_key            | meta_value                                 |
+---------+---------+---------------------+--------------------------------------------+
|   57594 |    3438 | name                |                                            |
|   57595 |    3438 | email               | test@test                                  |
|   57596 |    3438 | message             | nope                                       |
|   57597 |    3438 | referrer            | https://wp.local/libre-forms/test-form/    |
|   57598 |    3438 | _referrer_id        | 3435                                       |
|   57599 |    3438 | _form_id            | 3435                                       |
|   57600 |    3438 | _wplf_email_copy_to | vagrant@wp.local                           |
|   57681 |    3438 | _edit_lock          | 1516178533:2                               |
|   57682 |    3438 | _edit_last          | 2                                          |
|   57683 |    3438 | _relevanssi_pin     |                                            |
|   57684 |    3438 | _relevanssi_unpin   |                                            |
+---------+---------+---------------------+--------------------------------------------+
11 rows in set (0.00 sec)

WordPress handles fields in the post meta prefixed with _ as hidden fields, that are meant for internal use only. WordPress itself stores keys such as _edit_lock in the meta. This is just the way WordPress works & this plugin does things the WordPress way, and hiding fields that WordPress classifies as hidden is a sane default behavior.

Any of the fields prefixed with _ wouldn't make any sense in the export, as you should always only export submissions from one form at a time, so you should know what the form was, and other plugins such as Relevanssi or ACF might add their own meta to the submission.

Referrer happens to be saved with the submission, just because you didn't know it exists doesn't mean it shouldn't be exported. The user doesn't have to know everything about the system in order to use it.

Most of the clients I've worked don't even care about the actual exported form data, but just the total amount of leads generated by a page, as it helps them create better, more engaging content.

How do you count the amount of leads generated by a page, if you can't see what page was used to submit the form?