diff --git a/classes/controllers/FrmDeactivationFeedbackController.php b/classes/controllers/FrmDeactivationFeedbackController.php new file mode 100644 index 0000000000..e853678224 --- /dev/null +++ b/classes/controllers/FrmDeactivationFeedbackController.php @@ -0,0 +1,131 @@ +id; + } + + /** + * Checks if feedback is expired. + * + * @return bool + */ + private static function feedback_is_expired() { + $feedback_expired = get_option( 'frm_feedback_expired' ); + if ( ! $feedback_expired ) { + return true; + } + + $expired_date = strtotime( $feedback_expired ); + if ( ! $expired_date ) { + return true; + } + + return $expired_date < time(); + } + + /** + * Sets feedback expired date. + * + * @param string $plugin Path to the plugin file relative to the plugins directory. + * + * @return void + */ + public static function set_feedback_expired_date( $plugin ) { + if ( empty( $_GET['frm_feedback_submitted'] ) ) { + return; + } + if ( ! strpos( $plugin, 'formidable.php' ) && ! strpos( $plugin, 'formidable-pro.php' ) ) { + return; + } + update_option( 'frm_feedback_expired', gmdate( 'Y-m-d', strtotime( '+ 1 day' ) ) ); + } + + /** + * Enqueues assets. + * + * @return void + */ + public static function enqueue_assets() { + if ( ! self::is_plugins_page() || ! self::feedback_is_expired() ) { + return; + } + + wp_enqueue_script( + 'frm-deactivation-feedback', + FrmAppHelper::plugin_url() . '/js/admin/deactivation-feedback.js', + array( 'formidable', 'formidable_dom', 'jquery' ), + FrmAppHelper::plugin_version(), + true + ); + + wp_enqueue_style( 'formidable-admin' ); + + wp_enqueue_style( + 'frm-deactivation-feedback', + FrmAppHelper::plugin_url() . '/css/admin/deactivation-feedback.css', + array( 'formidable-admin' ), + FrmAppHelper::plugin_version() + ); + + FrmAppHelper::localize_script( 'front' ); + + wp_localize_script( + 'frm-deactivation-feedback', + 'FrmDeactivationFeedbackI18n', + array( + 'skip_text' => __( 'Skip & Deactivate', 'formidable' ), + ) + ); + } + + /** + * Prints footer HTML. + * + * @return void + */ + public static function footer_html() { + if ( ! self::is_plugins_page() || ! self::feedback_is_expired() ) { + return; + } + ?> +
+
+
+ + + + + + +
+ +
+ +
+
+
+
+
+ { + const btn = frmDom.a( { + text: FrmDeactivationFeedbackI18n.skip_text, + href: deactivationUrl, + className: 'frm-skip-link' + }); + + formEl.querySelector( '.frm_submit' ).prepend( btn ); + }; + + const onClickDeactivate = event => { + event.preventDefault(); + + if ( ! deactivationModal ) { + deactivationModal = Modal.init( + '#frm-deactivation-modal', + '440px' + ); + } + + deactivationUrl = event.target.href + '&frm_feedback_submitted=1'; + + const pluginSlug = event.target.closest( 'tr' ).dataset.slug; + + const url = 'https://feedback.strategy11.com/wp-json/frm/v2/forms/deactivation-feedback?plugin_slug=' + pluginSlug + '&site=' + window.location.host + '&return=html&exclude_script=jquery&exclude_style=formidable-css'; + + const response = fetch( url, { + method: 'GET' + }); + + response + .then( response => response.json() ) + .then( response => { + const wrapper = document.getElementById( 'frm-deactivation-form-wrapper' ); + const form = response.renderedHtml.replace( /]*(formidableforms.css|action=frmpro_css)[^>]*>/gi, '' ); + + jQuery( wrapper ).html( form ); + wrapper.setAttribute( 'data-slug', pluginSlug ); + addSkipBtn( wrapper ); + deactivationModal.dialog( 'open' ); + }) + .catch( error => { + console.error( error ); + }); + }; + + frmDom.util.documentOn( 'click', selectors, onClickDeactivate ); + + document.addEventListener( 'frmFormCompleteBeforeReplace', function( event ) { + document.getElementById( 'frm-deactivation-modal-icon' ).remove(); + window.location.href = deactivationUrl; + }); +}() );