Skip to content

Commit 63d348b

Browse files
authored
Save plugin version to forms (#104)
* Store plugin version on form creation * Prompt user to update form version and update if user accepts * Change version
1 parent 201433c commit 63d348b

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

assets/scripts/wplf-admin-form.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ $(document).ready(function() {
6363
}
6464
$('input[name="wplf_email_copy_enabled"]').change(toggleEmailCopy);
6565
toggleEmailCopy();
66+
67+
// If prompted for a form version update, create a hidden field if necessary
68+
function toggleVersionUpdate() {
69+
var hiddenField = $('input[name="wplf_update_plugin_version_to_meta"]');
70+
71+
if (hiddenField.length) {
72+
hiddenField.remove();
73+
return;
74+
}
75+
76+
var checkbox = document.createElement('input')
77+
checkbox.type = 'hidden';
78+
checkbox.name = 'wplf_update_plugin_version_to_meta';
79+
checkbox.value = 1;
80+
81+
$('#content').after(checkbox);
82+
}
83+
84+
$('input[name="wplf_version_update_toggle"]').change(toggleVersionUpdate);
6685
});
6786

6887
})(jQuery);

classes/class-cpt-wplf-form.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function __construct() {
2929
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes_cpt' ) );
3030
add_action( 'add_meta_boxes', array( $this, 'maybe_load_imported_template' ), 10, 2 );
3131
add_action( 'admin_enqueue_scripts', array( $this, 'admin_post_scripts_cpt' ), 10, 1 );
32+
add_action( 'admin_notices', array( $this, 'print_notices' ), 10 );
3233

3334
// edit.php view
3435
add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
@@ -179,6 +180,45 @@ public function admin_post_scripts_cpt( $hook ) {
179180
wp_enqueue_style( 'wplf-form-edit-css', $assets_url . '/styles/wplf-admin-form.css' );
180181
}
181182

183+
public function print_notices() {
184+
$post_id = ! empty( $_GET['post'] ) ? (int) $_GET['post'] : false;
185+
$version_created_at = get_post_meta( $post_id, '_wplf_plugin_version', true );
186+
$version_created_at = $version_created_at ? $version_created_at : '< 1.5';
187+
188+
// The notice prints outside the form element
189+
// a hidden field is created or deleted when this checkbox changes
190+
if ( version_compare( $version_created_at, WPLF_VERSION, '<' ) ) { ?>
191+
<div class="notice notice-info">
192+
<p>
193+
<?php echo sprintf(
194+
esc_html(
195+
// translators: Placeholders indicate version numbers
196+
__( 'This form was created with WPLF version %1$s, and your installed WPLF version is %2$s', 'wp-libre-form' )
197+
),
198+
esc_html( $version_created_at ),
199+
esc_html( WPLF_VERSION )
200+
); ?>
201+
</p>
202+
203+
<p>
204+
<?php echo esc_html(
205+
__( 'There might be new features available, would you like to update the form version?', 'wp-libre-form' )
206+
); ?>
207+
</p>
208+
209+
<p>
210+
<label>
211+
<input type="checkbox" name="wplf_version_update_toggle" value="1">
212+
<?php echo esc_html(
213+
__( 'Yes, update when I save the form', 'wp-libre-form' )
214+
); ?>
215+
</label>
216+
</p>
217+
</div>
218+
<?php
219+
}
220+
}
221+
182222

183223
/**
184224
* Pre-populate form editor with default content
@@ -765,6 +805,12 @@ public function save_cpt( $post_id ) {
765805
// should be fine to save the meta field without further sanitisaton
766806
update_post_meta( $post_id, '_wplf_title_format', $safe_title_format );
767807
}
808+
809+
// save plugin version, update if allowed
810+
$version = get_post_meta( $post_id, '_wplf_plugin_version', true );
811+
if ( ! $version || isset( $_POST['wplf_update_plugin_version_to_meta'] ) ) {
812+
update_post_meta( $post_id, '_wplf_plugin_version', WPLF_VERSION );
813+
}
768814
}
769815

770816

wp-libre-form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
if ( ! class_exists( 'WP_Libre_Form' ) ) :
3434

35-
define( 'WPLF_VERSION', '1.4.3' );
35+
define( 'WPLF_VERSION', '1.5.0-beta' );
3636

3737
class WP_Libre_Form {
3838
public static $instance;

0 commit comments

Comments
 (0)