From 733d3ffd6e7ced1dae6193d7e9c00ddf297feab5 Mon Sep 17 00:00:00 2001 From: David Smith Date: Thu, 17 Feb 2022 16:43:09 -0500 Subject: [PATCH] Create gpmpn-page-permalinks.php --- .../gpmpn-page-permalinks.php | 221 ++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 gp-multi-page-navigation/gpmpn-page-permalinks.php diff --git a/gp-multi-page-navigation/gpmpn-page-permalinks.php b/gp-multi-page-navigation/gpmpn-page-permalinks.php new file mode 100644 index 000000000..ed6dd4d33 --- /dev/null +++ b/gp-multi-page-navigation/gpmpn-page-permalinks.php @@ -0,0 +1,221 @@ +_args = wp_parse_args( $args, array( + 'form_id' => false, + 'permalinks' => array(), + ) ); + + // do version check in the init to make sure if GF is going to be loaded, it is already loaded + add_action( 'init', array( $this, 'init' ) ); + + } + + public function init() { + + // @todo For some reason after this runs for newly added page permalinks, all post permalinks are broken + add_filter( 'option_rewrite_rules', array( $this, 'hotload_rewrite_rules' ) ); + add_action( 'gpmpn_default_page', array( $this, 'set_default_form_page' ) ); + + add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); + add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 ); + + $this->add_query_var(); + $this->add_rewrite_rules(); + + } + + public function add_query_var() { + global $wp; + $wp->add_query_var( 'gpmpn_page' ); + } + + public function add_rewrite_rules() { + foreach ( $this->_args['permalinks'] as $page => $permalink ) { + add_rewrite_rule( "^{$this->_args['pagename']}/{$permalink}?", "index.php?pagename={$this->_args['pagename']}&gpmpn_page={$page}", 'top' ); + } + } + + public function hotload_rewrite_rules( $rules ) { + static $did_rewrite_rules; + + if ( ! is_array( $rules ) ) { + $rules = array(); + } + + $needs_flush = false; + + foreach ( $this->_args['permalinks'] as $page => $permalink ) { + if ( ! array_key_exists( "^{$this->_args['pagename']}/{$permalink}?", $rules ) ) { + $rules = array( "^{$this->_args['pagename']}/{$permalink}?" => "index.php?pagename={$this->_args['pagename']}&gpmpn_page={$page}" ) + $rules; + $needs_flush = true; + } + } + +// if ( $needs_flush ) { +// if ( ! $did_rewrite_rules ) { +// $did_rewrite_rules = true; +// $this->flush_rewrite_rules(); +// } +// } + + return $rules; + } + + public function flush_rewrite_rules() { + global $wp_rewrite; + $wp_rewrite->flush_rules(); + } + + public function set_default_form_page( $page ) { + return get_query_var( 'gpmpn_page' ); + } + + public function load_form_script( $form, $is_ajax_enabled ) { + + if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { + add_action( 'wp_footer', array( $this, 'output_script' ) ); + add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); + } + + return $form; + } + + public function output_script() { + ?> + + + + is_applicable_form( $form ) ) { + return; + } + + $args = array( + 'formId' => $this->_args['form_id'], + 'pagename' => $this->_args['pagename'], + 'permalinks' => $this->_args['permalinks'], + 'defaultPage' => get_query_var( 'gpmpn_page' ), + ); + + $script = 'if ( typeof window.GPMPNPagePermalinks !== \'undefined\' ) { new GPMPNPagePermalinks( ' . json_encode( $args ) . ' ); }'; + $slug = implode( '_', array( 'gpmpn_page_permalinks', $this->_args['form_id'] ) ); + + GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); + + } + + public function is_applicable_form( $form ) { + + $form_id = isset( $form['id'] ) ? $form['id'] : $form; + + return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id']; + } + +} + +# Configuration + +new GPMPN_Page_Permalinks( array( + 'form_id' => 889, + 'pagename' => 'permalinks-for-form-pages', + 'permalinks' => array( + 1 => 'page-one', + 2 => 'page-two', + 3 => 'page-three', + 0 => 'confirmation' + ), +) );