igmoweb
11/25/2015 - 5:50 PM

Gravity Forms + New Blog Templates Integration

Gravity Forms + New Blog Templates Integration

<?php
// This should add a new option in the form feed but it doesn't anymore because the action has dissappeared
add_action( 'gform_user_registration_add_option_section', 'nbt_add_blog_templates_user_registration_option', 15 );
function nbt_add_blog_templates_user_registration_option( $config ) {
  // I don't know if this would be valid in the new version
	$multisite_options = rgar($config['meta'], 'multisite_options');
  $my_option = rgar( $multisite_options, 'blog_templates' );
  ?>
    <input type="checkbox" id="gf_user_registration_multisite_blog_templates" name="gf_user_registration_multisite_blog_templates" value="1" <?php checked( rgar( $multisite_options, 'blog_templates' ) ); ?> />
  <?php
}

// This should save the option but again, the filter is missing in beta 3
add_filter( "gform_user_registration_save_config", "nbt_save_multisite_user_registration_config" );
function nbt_save_multisite_user_registration_config( $config ) {	
  // Does it make sense to use RGForms class anymore ?
	$config['meta']['multisite_options']['blog_templates'] = RGForms::post("gf_user_registration_multisite_blog_templates");
	return $config;
}

// This displays our plugin stuff in the registration form depending on the saved value
add_filter( 'gform_get_form_filter', 'nbt_render_user_registration_form', 15, 2 );
function nbt_render_user_registration_form( $form_html, $form ) {
  // I cannot use this method to get the feed anymore, what can I do now?
	$config = GFUserData::get_feed_by_form( $form['id'] );
	
	if ( empty( $config ) )
		return $form_html;
	
	// Get just the first feed
	$config = current( $config );
	
	$multisite_options = rgar( $config['meta'], 'multisite_options' );
	$my_option = $multisite_options['blog_templates'];
	
	if ( $my_option ) {
	  // Display some stuff, for instance, a hidden field
	  ?><input type="hidden" name="my-plugin-field" value="gf-please-fix-this"<?php
	}
}

// These two filters will process all that stuff that was displayed in nbt_render_user_registration_form()
// the value will be returned in the meta array and will be saved in wp_signup table later
// It's also triggered when a signup is being activated
add_filter( 'gform_user_registration_new_site_meta', 'nbt_save_new_blog_meta' );
add_filter( 'gform_user_registration_signup_meta', 'nbt_save_new_blog_meta' );
function nbt_save_new_blog_meta( $meta ) {
  if isset( $_POST['my-plugin-field'] )
    $meta['my-plugin-meta'] = $_POST['my-plugin-field'];
    
  // If GF is activating the signup, we need to take the meta that we already saved before
	if ( empty( $meta['my-plugin-meta'] ) && isset( $_REQUEST['key'] ) && class_exists( 'GFSignup' ) ) {
	  // Will this work for the beta 3? I hope so
	  $signup = GFSignup::get( $_REQUEST['key'] );
	  // We should check if ! is_wp_error() and all that jazz, but I removed that part for better clarity
	  $meta['my-plugin-meta'] = $signup->meta['my-plugin-meta'];
	}
	
	return $meta;
}