Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop alexey #1

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
.vscode
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# WP-Stateless - Gravity Forms Signature Addon

Provides compatibility between the [Gravity Forms Signature](https://www.gravityforms.com/add-ons/signature/) and the [WP-Stateless](https://wordpress.org/plugins/wp-stateless/) plugins.

### Features

* Sync signature files created with Gravity Forms Signature with Google Cloud Storage.

### Notes

* Tested with Gravity Forms Signature plugin version 4.6.0

### Support, Feedback, & Contribute

We welcome community involvement via the [GitHub repository](https://github.com/udx/wp-stateless-gravity-forms-signature-addon).

### Frequently Asked Questions

<details>
<summary>Where can I submit feature requests or bug reports?</summary>

We encourage community feedback and discussion through issues on the [GitHub repository](https://github.com/udx/wp-stateless-gravity-forms-signature-addon/issues).
</details>

<details>
<summary>Can I test new features before they are released?</summary>

To ensure new releases cause as little disruption as possible, we rely on early adopters who assist us by testing out new features before they are released. [Please contact us](https://udx.io/) if you are interested in becoming an early adopter.
</details>

<details>
<summary>Who maintains this plugin?</summary>

[UDX](https://udx.io/) maintains this plugin by continuing development through its own staff, reviewing pull requests, testing, and steering the overall release schedule. UDX is located in Durham, North Carolina, and provides WordPress engineering and hosting services to clients throughout the United States.
</details>
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== Changelog ==
= 0.0.1 =
* Initial public release.
3 changes: 3 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### 0.0.1

- Initial public release.
130 changes: 130 additions & 0 deletions class-gravity-forms-signature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace WPSL\GravityFormSignature;

use wpCloud\StatelessMedia\Compatibility;

class GravityFormSignature extends Compatibility {
protected $id = 'gravity-form-signature';
protected $title = 'Gravity Forms Signature Add-On';
protected $constant = 'WP_STATELESS_COMPATIBILITY_GF_SIG';
protected $description = 'Enables support for signature images generated by the signature add-on for Gravity Forms.';
protected $plugin_file = 'gravityformssignature/signature.php';
protected $plugin_version;

/**
* @param $sm
*/
public function module_init($sm) {
add_filter('gform_save_field_value', array($this, 'gform_save_field_value'), 10, 5);
add_filter('site_url', array($this, 'signature_url'), 10, 4);
add_filter('gform_signature_delete_file_pre_delete_entry', array($this, 'delete_signature'), 10, 4);
add_filter('gform_signature_url', array($this, 'get_signature_url'), 10, 4);
}

/**
* On gform save field value sync file to GCS and alter the file url to GCS link.
* @param $value
* @param $lead
* @param $field
* @param $form
* @param $input_id
* @return array|false|mixed|string
*/
public function gform_save_field_value($value, $lead, $field, $form, $input_id) {
if (empty($value)) return $value;

$type = \GFFormsModel::get_input_type($field);
if ($type == 'signature') {
/**
* Compatibility for Signature addon.
*/
try {
$folder = \GFSignature::get_signatures_folder();
$file_path = $folder . $value;

$name = apply_filters('wp_stateless_file_name', $file_path, false);
do_action('sm:sync::syncFile', $name, $file_path, true);
} catch (\Throwable $th) {
//throw $th;
}
}
return $value;
}

/**
* [older version of Gravity Forms Signature]
* Currently there is no way to filter signature url. So instead we are filtering site_url function
* with help of debug backtrace.
*
* Also doing sync on the fly for previous entries.
*/
public function signature_url($url, $path, $scheme, $blog_id) {
try {
$db = debug_backtrace(false, 7);
foreach ($db as $key => $value) {
if ($value['function'] == 'get_signature_url' && rgar($value, 'class') == 'GFSignature') {
$folder = \GFSignature::get_signatures_folder();
$name = $value['args'][0];
$file_path = $folder . $name . '.png';
$name = apply_filters('wp_stateless_file_name', $file_path);
do_action('sm:sync::syncFile', $name, $file_path);
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
break;
}
}
} catch (\Throwable $th) {
//throw $th;
}
return $url;
}

/**
* [newer version of Gravity Forms Signature]
* Get GSC URL for signature image.
*
* Also doing sync on the fly for previous entries.
*/
public function get_signature_url($url, $filename, $form_id, $field_id) {
$mode = ud_get_stateless_media()->get('sm.mode');

if ( $mode === 'disabled' ) {
return $url;
}

// Sync signature file
try {
$folder = \GFSignature::get_signatures_folder();
$absolute_path = $folder . $filename . '.png';
$name = apply_filters('wp_stateless_file_name', $absolute_path, false);
do_action('sm:sync::syncFile', $name, $absolute_path);

if ( $mode !== 'backup' ) {
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
}
} catch (\Throwable $th) {
//throw $th;
}

return $url;
}

/**
* Deleting signature file from GCS.
*/
public function delete_signature($return, $form, $lead_id, $field_id) {
try {
$lead = \RGFormsModel::get_lead($lead_id);
$folder = \GFSignature::get_signatures_folder();

$name = rgar($lead, $field_id);
$file_path = $folder . $name;

$name = apply_filters('wp_stateless_file_name', $file_path);
do_action('sm:sync::deleteFile', $name);
} catch (\Throwable $th) {
//throw $th;
}
return $return;
}
}
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "udx/wp-stateless-gravity-forms-signature-addon",
"description": "Ensures compatibility with Gravity Forms Signature",
"type": "wordpress-plugin",
"license": "MIT",
"authors": [
{
"name": "UDX",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "^9.5",
"brain/monkey": "2.*"
},
"autoload": {
"files": [
"class-gravity-forms-signature.php"
]
},
"scripts": {
"test": [
"@composer install",
"./vendor/bin/phpunit --prepend tests/prepend.php tests/ --testdox"
]
},
"scripts-descriptions": {
"test": "Run all tests."
}
}
Loading