Skip to content

Commit 39760d5

Browse files
authored
Merge pull request #78 from Kit/fix-tests-creator-network
Creator Network: Save Settings on WPForms Lite 1.9.4.0+
2 parents 69fc061 + e808e17 commit 39760d5

12 files changed

+156
-107
lines changed

.env.dist.testing

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TEST_SITE_ADMIN_USERNAME=admin
88
TEST_SITE_ADMIN_PASSWORD=password
99
TEST_SITE_ADMIN_EMAIL="[email protected]"
1010
TEST_SITE_WP_ADMIN_PATH=/wp-admin
11-
WP_ROOT_FOLDER="/home/runner/work/convertkit-wpforms/convertkit-wpforms/wordpress"
11+
WP_ROOT_FOLDER="/var/www/html"
1212
TEST_DB_NAME=test
1313
TEST_DB_HOST=localhost
1414
TEST_DB_USER=root

.github/workflows/coding-standards.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
# Virtual Environment to use.
1919
# @see: https://github.com/actions/virtual-environments
20-
runs-on: ubuntu-20.04
20+
runs-on: ubuntu-latest
2121

2222
# Environment Variables.
2323
# Accessible by using ${{ env.NAME }}

.github/workflows/deploy-readme.yml

-18
This file was deleted.

.github/workflows/deploy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
# Virtual Environment to use
1313
# @see: https://github.com/actions/virtual-environments
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-latest
1515

1616
# Steps to deploy
1717
steps:

.github/workflows/tests.yml

+69-18
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ jobs:
1717

1818
# Virtual Environment to use.
1919
# @see: https://github.com/actions/virtual-environments
20-
runs-on: ubuntu-20.04
20+
runs-on: ubuntu-latest
2121

2222
# Environment Variables.
2323
# Accessible by using ${{ env.NAME }}
2424
# Use ${{ secrets.NAME }} to include any GitHub Secrets in ${{ env.NAME }}
2525
# The base folder will always be /home/runner/work/github-repo-name/github-repo-name
2626
env:
27-
ROOT_DIR: /home/runner/work/convertkit-wpforms/convertkit-wpforms/wordpress
28-
PLUGIN_DIR: /home/runner/work/convertkit-wpforms/convertkit-wpforms/wordpress/wp-content/plugins/convertkit-wpforms
27+
ROOT_DIR: /var/www/html
28+
PLUGIN_DIR: /var/www/html/wp-content/plugins/convertkit-wpforms
2929
DB_NAME: test
3030
DB_USER: root
3131
DB_PASS: root
@@ -72,6 +72,13 @@ jobs:
7272
replace: '-'
7373
replaceAll: true
7474

75+
# Checkout Plugin to /home/runner/work/convertkit-wpforms/convertkit-wpforms/convertkit-wpforms
76+
# We cannot checkout to ${{ env.PLUGIN_DIR }} as GitHub Actions require it be first placed in /home/runner/work/repo/repo
77+
- name: Checkout Plugin
78+
uses: actions/checkout@v4
79+
with:
80+
path: /home/runner/work/convertkit-wpforms/convertkit-wpforms/convertkit-wpforms
81+
7582
- name: Start MySQL
7683
run: sudo systemctl start mysql.service
7784

@@ -86,17 +93,15 @@ jobs:
8693

8794
# Some workflows checkout WordPress from GitHub, but that seems to bring a bunch of uncompiled files with it.
8895
# Instead download from wordpress.org stable.
89-
- name: Download WordPress
90-
run: wget https://wordpress.org/wordpress-${{ matrix.wp-versions }}.tar.gz
91-
92-
- name: Extract WordPress
93-
run: tar xfz wordpress-${{ matrix.wp-versions }}.tar.gz
94-
95-
# Checkout (copy) this repository's Plugin to this VM.
96-
- name: Checkout Plugin
97-
uses: actions/checkout@v4
98-
with:
99-
path: ${{ env.PLUGIN_DIR }}
96+
- name: Download and Extract WordPress
97+
run: |
98+
sudo chown -R runner:docker /var/www/html
99+
ls -la /var/www/html
100+
cd /var/www/html
101+
wget https://wordpress.org/wordpress-${{ matrix.wp-versions }}.tar.gz
102+
tar xfz wordpress-${{ matrix.wp-versions }}.tar.gz
103+
mv wordpress/* .
104+
rm -rf wordpress wordpress-${{ matrix.wp-versions }}.tar.gz
100105
101106
# We install WP-CLI, as it provides useful commands to setup and install WordPress through the command line.
102107
- name: Install WP-CLI
@@ -118,6 +123,10 @@ jobs:
118123
working-directory: ${{ env.ROOT_DIR }}
119124
run: wp-cli plugin install ${{ env.INSTALL_PLUGINS }}
120125

126+
# Move Plugin
127+
- name: Move Plugin
128+
run: mv /home/runner/work/convertkit-wpforms/convertkit-wpforms/convertkit-wpforms ${{ env.PLUGIN_DIR }}
129+
121130
# WP_DEBUG = true is required so all PHP errors are output and caught by tests (E_ALL).
122131
- name: Enable WP_DEBUG
123132
working-directory: ${{ env.ROOT_DIR }}
@@ -139,10 +148,37 @@ jobs:
139148
php-version: ${{ matrix.php-versions }}
140149
coverage: xdebug
141150

142-
# Make sure that an nginx configuration file exists in this repository at tests/nginx/php-x.x.conf.
143-
# Refer to an existing .conf file in this repository if you need to create a new one e.g. for a new PHP version.
144-
- name: Copy nginx configuration file
145-
run: sudo cp ${{ env.PLUGIN_DIR }}/tests/nginx/php-${{ matrix.php-versions }}.conf /etc/nginx/conf.d/php-${{ matrix.php-versions }}.conf
151+
# Configure nginx to use the PHP version and WOrdPress installation at /var/www/html
152+
- name: Configure nginx site
153+
run: |
154+
sudo rm -f /etc/nginx/sites-enabled/default
155+
sudo tee /etc/nginx/sites-available/default > /dev/null << 'EOF'
156+
157+
server {
158+
listen 80 default_server;
159+
listen [::]:80 default_server;
160+
161+
root /var/www/html;
162+
index index.php;
163+
164+
server_name localhost;
165+
166+
location / {
167+
try_files $uri $uri/ /index.php?$args;
168+
}
169+
170+
location ~ \.php$ {
171+
include snippets/fastcgi-php.conf;
172+
fastcgi_pass unix:/run/php/php${{ matrix.php-versions }}-fpm.sock;
173+
}
174+
175+
location ~ /\.ht {
176+
deny all;
177+
}
178+
}
179+
EOF
180+
181+
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default || true
146182
147183
- name: Test nginx
148184
run: sudo nginx -t
@@ -190,6 +226,21 @@ jobs:
190226
working-directory: ${{ env.PLUGIN_DIR }}
191227
run: composer dump-autoload
192228

229+
# This ensures that applicable files and folders can be written to by WordPress and cache Plugins.
230+
- name: Set File and Folder Permissions
231+
run: |
232+
sudo chmod 767 ${{ env.ROOT_DIR }}
233+
sudo chown www-data:www-data ${{ env.ROOT_DIR }}
234+
235+
sudo chmod 767 ${{ env.ROOT_DIR }}/wp-config.php
236+
sudo chown www-data:www-data ${{ env.ROOT_DIR }}/wp-config.php
237+
238+
sudo chmod 767 ${{ env.ROOT_DIR }}/wp-content
239+
sudo chown www-data:www-data ${{ env.ROOT_DIR }}/wp-content
240+
241+
sudo chmod -R 767 ${{ env.ROOT_DIR }}/wp-content/uploads
242+
sudo chown www-data:www-data ${{ env.ROOT_DIR }}/wp-content/uploads
243+
193244
# This ensures the Plugin's log file can be written to.
194245
# We don't recursively do this, as it'll prevent Codeception from writing to the /tests/_output directory.
195246
- name: Set Permissions for Plugin Directory

includes/class-integrate-convertkit-wpforms-creator-network-recommendations.php

+80-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ class Integrate_ConvertKit_WPForms_Creator_Network_Recommendations {
6060
*/
6161
private $convertkit_billing_url = 'https://app.kit.com/account_settings/billing/?utm_source=wordpress&utm_content=convertkit-wpforms';
6262

63+
/**
64+
* Holds the creator network settings that were defined when saving
65+
* a WPForms Form.
66+
*
67+
* @since 1.8.0
68+
*
69+
* @var bool|array
70+
*/
71+
private $creator_network_settings = false;
72+
6373
/**
6474
* Constructor
6575
*
@@ -68,7 +78,9 @@ class Integrate_ConvertKit_WPForms_Creator_Network_Recommendations {
6878
public function __construct() {
6979

7080
add_filter( 'wpforms_builder_settings_sections', array( $this, 'settings_section' ), 20, 1 );
71-
add_filter( 'wpforms_form_settings_panel_content', array( $this, 'settings_section_content' ), 20 );
81+
add_action( 'wpforms_form_settings_panel_content', array( $this, 'settings_section_content' ), 20 );
82+
add_filter( 'wpforms_save_form_args', array( $this, 'store_settings_on_form_edit' ), 10, 2 );
83+
add_action( 'wpforms_save_form', array( $this, 'apply_stored_settings_on_form_save' ) );
7284
add_action( 'wpforms_frontend_js', array( $this, 'maybe_enqueue_creator_network_recommendations_script' ) );
7385

7486
}
@@ -226,6 +238,73 @@ public function settings_section_close_and_return() {
226238

227239
}
228240

241+
/**
242+
* Store the form values for Creator Network Recommendations when the Form's save button is clicked,
243+
* immediately before the form is saved in WPForms.
244+
*
245+
* @since 1.8.0
246+
*
247+
* @param array $post_data WPForms Form posted data on save.
248+
* @param array $form_data WPForms Form settings data posted on save.
249+
* @return array
250+
*/
251+
public function store_settings_on_form_edit( $post_data, $form_data ) {
252+
253+
// Define the settings which will be saved to the WPForms Form.
254+
$this->creator_network_settings = array(
255+
$this->connection_id_key => ( isset( $form_data['settings'][ $this->connection_id_key ] ) ? $form_data['settings'][ $this->connection_id_key ] : '' ),
256+
$this->creator_network_recommendations_script_key => ( isset( $form_data['settings'][ $this->creator_network_recommendations_script_key ] ) ? $form_data['settings'][ $this->creator_network_recommendations_script_key ] : '' ),
257+
);
258+
259+
return $post_data;
260+
261+
}
262+
263+
/**
264+
* Apply the form values for Creator Network Recommendations after the form is saved in WPForms,
265+
* because WPForms Lite 1.9.4.0 and higher introduces a nasty bug where changes made to settings
266+
* on an existing WPForms Form won't be honored.
267+
*
268+
* @param int $form_id WPForms Form ID.
269+
*
270+
* @since 1.8.0
271+
*/
272+
public function apply_stored_settings_on_form_save( $form_id ) {
273+
274+
// Bail if Creator Network settings isn't an array.
275+
if ( ! $this->creator_network_settings ) {
276+
return;
277+
}
278+
279+
// Get Form.
280+
$form = get_post( $form_id );
281+
282+
// Bail if the Form could not be fetched.
283+
if ( ! $form ) {
284+
return;
285+
}
286+
287+
// Decode WPForms Form settings.
288+
$data = wpforms_decode( $form->post_content );
289+
290+
// Bail if the WPForms Form settings could not be decoded.
291+
if ( ! $data ) {
292+
return;
293+
}
294+
295+
// Merge settings.
296+
$data['settings'] = array_merge( $data['settings'], $this->creator_network_settings );
297+
298+
// Update Post.
299+
wp_update_post(
300+
array(
301+
'ID' => $form_id,
302+
'post_content' => wpforms_encode( $data ),
303+
)
304+
);
305+
306+
}
307+
229308
/**
230309
* Enqueues the Creator Network Recommendations script, if the WPForms form
231310
* has the 'Enable Creator Network Recommendations' setting enabled.

tests/acceptance/general/UpgradePathsCest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ public function testFormSettingsPrefixAddedToSettingsOnUpgrade(AcceptanceTester
109109
// Other required settings.
110110
'field_id' => '0',
111111
'settings' => [
112-
'store_spam_entries' => '0',
113-
'submit_text' => 'Submit',
112+
'store_spam_entries' => '0',
113+
'submit_text' => 'Submit',
114+
'convertkit_connection_id' => '',
115+
'convertkit_wpforms_creator_network_recommendations' => '',
114116
],
115117
];
116118

tests/nginx/php-7.4.conf

-13
This file was deleted.

tests/nginx/php-8.0.conf

-13
This file was deleted.

tests/nginx/php-8.1.conf

-13
This file was deleted.

tests/nginx/php-8.2.conf

-13
This file was deleted.

tests/nginx/php-8.3.conf

-13
This file was deleted.

0 commit comments

Comments
 (0)