Commit 6621c271 authored by Christian Wach's avatar Christian Wach
Browse files

Initial code commit

parent 8b926bf2
# EditorConfig is awesome: https://editorconfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
# Tab indentation
[*.php]
indent_style = tab
This diff is collapsed.
# Submission Logs for ACFE
Logs submissions to ACFE Forms
Logs submissions for ACFE Forms to a file or the database.
## Getting started
---
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
### Description
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
*Submission Logs for ACFE* is a WordPress plugin that logs submissions for ACFE Forms to a file or the database.
## Add your files
The destination for the submission data (database of file) can be chosen by setting the `SLFA_METHOD` constant to either 'db' or 'file' before activating the plugin. Defaults to 'file'.
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
### Requirements
```
cd existing_repo
git remote add origin https://develop.tadpole.cc/plugins/submission-logs-for-acfe.git
git branch -M main
git push -uf origin main
```
* [Advanced Custom Fields Pro](https://www.advancedcustomfields.com/pro/)
* [ACF Extended](https://wordpress.org/plugins/acf-extended/)
## Integrate with your tools
### Installation
- [ ] [Set up project integrations](https://develop.tadpole.cc/plugins/submission-logs-for-acfe/-/settings/integrations)
There are two ways to install from GitLab:
## Collaborate with your team
##### ZIP Download
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
If you have downloaded *Submission Logs for ACFE* as a ZIP file from the GitLab repository, do the following to install and activate the plugin:
## Test and Deploy
1. Unzip the .zip file and rename the enclosing folder so that the plugin's files are located directly inside `/wp-content/plugins/submission-logs-for-acfe`.
2. Set the `SLFA_METHOD` constant to either 'db' or 'file'.
3. Activate the plugin.
4. You are done!
Use the built-in continuous integration in GitLab.
##### git clone
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
If you have cloned the code from GitLab, it is assumed that you know what you're doing.
<?php
/**
* ACFE Class.
*
* Handles ACFE functionality.
*
* @package Submission_Logs_For_ACFE
* @since 0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* ACFE Class.
*
* A class that encapsulates ACFE functionality.
*
* @since 0.1
*/
class SLFA_ACFE_Base {
/**
* Parsed Fields array.
*
* @since 0.1
* @access public
* @var object $parsed_fields The array of Parsed Fields.
*/
public $parsed_fields = [];
/**
* Constructor.
*
* @since 0.1
*/
public function __construct() {
// Register hooks.
$this->register_hooks_acfe();
}
/**
* Register ACFE hooks.
*
* @since 0.1
*/
public function register_hooks_acfe() {
// Register ACFE Form Actions.
add_action( 'acfe/form/submit', [ $this, 'form_submitted' ], 50, 2 );
}
/**
* Callback for Form Submissions.
*
* Form settings are important here: make sure you are clear about whether
* the Form should have "KSES" applied or not.
*
* @since 0.1
*
* @param array $form The ACFE Form data.
* @param integer $post_id The numeric ID of the WordPress Post.
*/
public function form_submitted( $form, $post_id ) {
// Get Form data.
$data = [];
$store = acf_get_store( 'acfe/form' );
if ( $store->has( 'data' ) ) {
$data = $store->get( 'data' );
}
// Bail if we have none.
if ( empty( $data ) ) {
return;
}
// Build Fields array.
$fields = [];
foreach ( $data as $field ) {
// Exclude reCAPTCHA Fields.
if ( $field['field']['type'] === 'acfe_recaptcha' ) {
continue;
}
// Exclude previously parsed Fields.
if ( in_array( $field['key'], $this->parsed_fields ) ) {
continue;
}
// Add formatted value to Fields array.
$fields[ $field['name'] ] = $this->field_to_array( $field );
}
// Let's have the User if there is one.
$user = wp_get_current_user();
$user_id = 0;
if ( $user instanceof WP_User ) {
$user_id = $user->ID;
}
// Build data to pass to save method.
$save = [
'form_id' => $form['ID'],
'name' => $form['name'],
//'title' => $form['title'],
//'form' => $form,
'user_id' => $user_id,
'fields' => $fields,
];
// Save data.
$this->form_data_save( $save );
}
/**
* Save Form Submission data.
*
* @since 0.1
*
* @param array $data The ACFE Form data array.
*/
public function form_data_save( $data ) {}
/**
* Build a nested array of Field data.
*
* @since 0.1
*
* @param array $field The ACFE Field data.
* @param array $parent_field The ACFE Parent Field data.
* @return array $array The nested ACFE Field data.
*/
public function field_to_array( $field, $parent_field = [] ) {
// Sanity check incoming arrays.
if ( is_array( $field['value'] ) ) {
// Check keys.
$keys_are_field_keys = true;
$keys = array_keys( $field['value'] );
foreach ( $keys as $key ) {
if ( ! acf_is_field_key( $key ) ) {
$keys_are_field_keys = false;
break;
}
}
// Return the decoded value if the keys are not Field keys.
if ( ! $keys_are_field_keys ) {
array_walk( $field['value'], function( &$item ) {
if ( is_string( $item ) && ! is_numeric( $item ) ) {
$item = html_entity_decode( $item );
}
} );
return $field['value'];
}
}
// Get raw Field value.
$acf_value = $field['value'];
// Init Sub-fields value.
$value = [];
// If the value is an array.
if ( is_array( $acf_value ) ) {
foreach ( $acf_value as $field_key => $field_value ) {
// Get the Sub-field.
$sub_field = acf_get_field( $field_key );
if ( ! $sub_field ) {
continue;
}
// Exclude reCAPTCHA Fields.
if ( $sub_field['type'] === 'acfe_recaptcha' ) {
continue;
}
// Apply value to Sub-field.
$sub_field['value'] = $field_value;
// Recurse when the value is an array.
if ( is_array( $field_value ) ) {
$value[ $sub_field['name'] ] = $this->field_to_array( $sub_field, $field );
} else {
if ( is_string( $field_value ) && ! is_numeric( $field_value ) ) {
$value[ $sub_field['name'] ] = html_entity_decode( $field_value );
} else {
$value[ $sub_field['name'] ] = $field_value;
}
}
// Add to parsed array.
$this->parsed_fields[] = $sub_field['key'];
}
}
// Overwrite with parsed value if it exists.
if ( ! empty( $value ) ) {
$acf_value = $value;
}
// Add to parsed array.
$this->parsed_fields[] = $field['key'];
// --<
return $acf_value;
}
} // Class ends.
<?php
/**
* Database Logging Class.
*
* Handles Database Logging functionality.
*
* @package Submission_Logs_For_ACFE
* @since 0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* File Logging Class.
*
* A class that encapsulates Database Logging functionality.
*
* @since 0.1
*/
class SLFA_Database_Logging extends SLFA_ACFE_Base {
/**
* Plugin object.
*
* @since 0.1
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* Constructor.
*
* @since 0.1
*
* @param object $plugin The plugin object.
*/
public function __construct( $plugin ) {
// Store references to objects.
$this->plugin = $plugin;
// Initialise immediately.
$this->initialise();
// Init parent.
parent::__construct();
}
/**
* Initialise this object.
*
* @since 0.1
*/
public function initialise() {
// Only do this once.
static $done;
if ( isset( $done ) && $done === true ) {
return;
}
// Register hooks.
$this->register_hooks();
/**
* Broadcast that this class is now loaded.
*
* @since 0.1
*/
do_action( 'slfa/logger/db/loaded' );
// We're done and loaded.
$done = true;
}
/**
* Register hooks.
*
* @since 0.1
*/
public function register_hooks() {
// Register WPDB changes.
add_action( 'plugins_loaded', [ $this, 'wpdb_fix' ], 1 );
add_action( 'switch_blog', [ $this, 'wpdb_fix' ] );
add_action( 'slfa/activated', [ $this, 'wpdb_fix' ] );
add_action( 'slfa/deactivated', [ $this, 'wpdb_fix' ] );
add_action( 'slfa/deleted', [ $this, 'wpdb_fix' ] );
// Register plugin hooks.
add_action( 'slfa/activated', [ $this, 'wpdb_install' ] );
// When testing, you can switch the following hooks to clear the table on deactivation.
//add_action( 'slfa/deactivated', [ $this, 'wpdb_uninstall' ] );
add_action( 'slfa/deleted', [ $this, 'wpdb_uninstall' ] );
}
/**
* Adds our log table to the database.
*
* @since 0.1
*/
public function wpdb_install() {
global $wpdb;
// Discover collation.
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = " DEFAULT CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE $wpdb->collate";
}
// Define log table.
$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->acfe_logs} (
id bigint(20) NOT NULL AUTO_INCREMENT,
datestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
form_id bigint(20) NOT NULL,
name varchar(255),
data longtext,
PRIMARY KEY (id),
KEY form_id (form_id)
)" . $charset_collate;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Removes our log table from the database.
*
* @since 0.1
*/
public function wpdb_uninstall() {
// Drop logs table.
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS $wpdb->acfe_logs" );
}
/**
* Adds log table reference to $wpdb global.
*
* @since 0.1
*/
public function wpdb_fix() {
global $wpdb;
$wpdb->acfe_logs = "{$wpdb->prefix}acfe_logs";
}
/**
* Saves the Form Submission data to the database.
*
* @since 0.1
*
* @param array $data The ACFE Form data array.
*/
public function form_data_save( $data ) {
global $wpdb;
// Build entry to save.
$entry = [
'form_id' => (int) $data['form_id'],
'name' => sanitize_key( $data['name'] ),
'data' => wp_json_encode( $data['fields'] ),
];
// Build format array.
$format = [ '%d', '%s', '%s' ];
// Go ahead and save.
$wpdb->insert( $wpdb->acfe_logs, $entry, $format );
}
} // Class ends.
<?php
/**
* File Logging Class.
*
* Handles File Logging functionality.
*
* @package Submission_Logs_For_ACFE
* @since 0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* File Logging Class.
*
* A class that encapsulates File Logging functionality.
*
* @since 0.1
*/
class SLFA_File_Logging extends SLFA_ACFE_Base {
/**
* Plugin object.
*
* @since 0.1
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* Constructor.
*
* @since 0.1
*
* @param object $plugin The plugin object.
*/
public function __construct( $plugin ) {
// Store references to objects.
$this->plugin = $plugin;
// Init parent.
parent::__construct();
/**
* Broadcast that this class is now loaded.
*
* @since 0.1
*/
do_action( 'slfa/logger/file/loaded' );
}
/**
* Saves the Form Submission data to a log file.
*
* @since 0.1
*
* @param array $data The ACFE Form data array.
*/
public function form_data_save( $data ) {
// Log to file.
$line = '[' . gmdate( 'd-M-Y H:i:s' ) . '] ' . wp_json_encode( $data ) . "\n";
error_log( $line, 3, SLFA_LOG_FILE );
}
} // Class ends.
# Copyright (C) 2022 Submission Logs for ACFE
# This file is distributed under the same license as the Submission Logs for ACFE package.
msgid ""
msgstr ""
"Project-Id-Version: Submission Logs for ACFE 0.1\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/submission-logs-"
"for-acfe\n"
"POT-Creation-Date: 2022-04-04 12:26:59+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"PO-Revision-Date: 2022-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
#. Plugin Name of the plugin/theme
msgid "Submission Logs for ACFE"
msgstr ""
#. Plugin URI of the plugin/theme
msgid "https://develop.tadpole.cc/plugins/submission-logs-for-acfe"
msgstr ""
#. Description of the plugin/theme
msgid "Logs submissions to ACF Extended Forms."
msgstr ""