Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active April 21, 2019 09:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wpmudev-sls/26bdfcf9be903e14acb0a3b9a9924dc5 to your computer and use it in GitHub Desktop.
[Pro Sites] - Custom Checkout Fields. Adds custom checkout fields for site and/or user in Pro Site's checout page
<?php
/**
* Plugin Name: [Pro Sites] - Custom Checkout Fields
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds custom checkout fields for site and/or user in Pro Site's checout page
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_PS_Custom_Checkout_Fields' ) ) {
class WPMUDEV_PS_Custom_Checkout_Fields {
private static $_instance = null;
private static $acceptable_field_types = array( 'raw_html', 'text', 'select', 'checkbox', 'shortcode' );
/**
* Holds the replacement fields. These are the 4 basic ones we need to replace.
* input_1 is the GF field for username
* input_2 is the GF field for email and
* input_7 is the sites's blogname (will use the same for blog_title)
*
* @since Unknown
* @access private
*
* @var array $switch_fields The fields to replace
*/
private static $switch_fields = array(
'user_name' => 'input_1',
'user_email' => 'input_3',
'blogname' => 'input_7',
'blog_title' => 'input_7'
);
/**
* Holds the additional meta fields. Here is an example for a custom meta for site_category and the site_audience. We will later on use it upon site creation.
* In our example here we also have some fields from a Gravity Form shortcode. The field we will keep in meta is the value of a Template Select field created in GF.
* That field contains 3 options for themes that will be active upon new site creation, Twenty Ninenteen (with value twenty-ninteen), Twenty Sevennteen (with value twenty-seventeen) and Twenty Sixteen (with value twenty-sixteen.
* That field's id is 8, os the field name should be `input_8`.
*
* @since Unknown
* @access private
*
* @var array $switch_fields The fields to replace
*/
private static $additional_meta = array(
'site_category',
'site_audience',
'input_8',
'input_2'
);
/**
* Contains the custom fields that will be in added in the user fields section. In this example we include two custom fields, site_category and site_audience.
* When we need to get those fields values we can use their keys
* Additionally we include a gravity forms shortcode. To get the GF fields values, we need to check the names of those input fields in the checkout page (unless there is some other way GF provides those names. Not familiar with GF).
*
* @since Unknown
* @access private
*
* @var array $user_fields The fields to insert in user section
*/
private static $user_fields = array();
/**
* Contains the custom fields that will be in added in the site fields section
*
* @since Unknown
* @access private
*
* @var array $site_fields The fields to insert for site
*/
private static $site_fields = array(
'site_category' => array(
'label_tile' => "Choose site category",
'wrapper_class' => 'ps-example-custom-field',
'class' => '',
'type' => 'select',
'value' => '',
'options' => array(
'business' => "Business",
'news' => "News",
'fashion' => "Fasion and trends"
)
),
'site_audience' => array(
'label_tile' => "Audience",
'wrapper_class' => 'ps-example-custom-field',
'class' => 'new-site-audience-checkbox',
'type' => 'checkbox',
'options' => array(
'hobbyists' => "Hobbyists",
'pros' => "Professional"
)
),
'gravity_form' => array(
'type' => 'shortcode',
'value' => '[gravityform id="1"]'
)
);
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_PS_Custom_Checkout_Fields();
}
return self::$_instance;
}
private function __construct() {
add_action( 'signup_blogform', array( $this, 'site_fields' ) );
add_action( 'signup_extra_fields', array( $this, 'user_fields' ) );
add_action( 'wp_ajax_check_prosite_blog', array( $this, 'ajax_check_prosite_blog' ), 9 );
add_action( 'wp_ajax_nopriv_check_prosite_blog', array( $this, 'ajax_check_prosite_blog' ), 9 );
add_action( 'add_signup_meta', array( $this, 'additional_meta' ) );
add_action( 'wpmu_activate_blog', array( $this, 'activate_blog' ), 20, 5 );
// Add css and js to hide and remove Default PS fields in checkout page
add_action( 'wp_head', array( $this, 'print_header_scripts' ) );
add_action( 'wp_footer', array( $this, 'print_footer_scripts' ) );
}
public function activate_blog( $blog_id, $user_id, $password, $signup_title, $meta ) {
if ( empty( self::$additional_meta ) ) {
return;
}
foreach ( self::$additional_meta as $meta_key ) {
// Do stuff for each custom meta.
// For example, we know that there should be a meta key called `input_8` which should contain the theme selected
// So lets set the selected template for new site created
if ( isset( $meta[ $meta_key ] ) ) {
if ( 'input_8' == $meta_key ) {
switch_to_blog( $blog_id );
switch_theme( $meta[ $meta_key ] );
restore_current_blog();
}
}
}
}
/**
* Add the custom meta in default meta array
*
* @since Unknown
* @access public
*
* @param array signup_meta The default signup meta
*
* @return array $signup_meta The site meta to be inserted into the signups table
*/
public function additional_meta( $signup_meta ) {
foreach ( self::$additional_meta as $meta_key ) {
if ( isset( $_POST[ $meta_key ] ) ) {
$signup_meta[ $meta_key ] = $_POST[ $meta_key ];
}
}
return $signup_meta;
}
public function ajax_check_prosite_blog() {
// Switch the $_POST data values
$data = array();
parse_str( $_POST['data'], $data );
foreach ( self::$switch_fields as $ps_key => $switch_key ) {
//if ( isset( $data[ $ps_key ] ) && isset( $data[ $switch_key ] ) ) {
if ( isset( $data[ $switch_key ] ) ) {
$data[ $ps_key ] = $data[ $switch_key ];
}
}
$_POST['data'] = http_build_query( $data );
}
public function print_header_scripts() {
global $psts;
if ( ! is_page() || get_the_ID() != $psts->get_setting( 'checkout_page' ) ) {
return;
}
?>
<style type="text/css">
#prosites-user-register div.username,
#prosites-user-register div.email,
#prosites-user-register div.blogname,
#prosites-user-register div.blog_title,
.gform_footer input[type=submit] {
display: none !important;
}
.ps-signup-errors {
width: 100%;
padding: 20px;
border: 2px solid #ef6e6e;
background-color: #ffd2d2;
}
.ps-signup-errors .error_item {
padding: 15 px;
color: #555;
font-size: 1.2 em;
}
</style>
<?php
}
public function print_footer_scripts() {
global $psts;
if ( ! is_page() || get_the_ID() != $psts->get_setting( 'checkout_page' ) ) {
return;
}
?>
<script type="text/javascript">
($=>{
WPMUDEV_PS_Custom_Checkout_Fields = {
load : function(){
$( '.gform_footer input[type=submit]').remove();
$( document ).ajaxComplete( WPMUDEV_PS_Custom_Checkout_Fields.check_signup_errors );
},
check_signup_errors : function( event, xhr, settings ){
let data = $( xhr.responseText.replace( "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>", '' ) ),
response = data.find( 'response' ),
clear_response = $(response.html().replace("<!--[CDATA[", "").replace("]]>", "") ),
response_data = $(clear_response).find( 'response_data' ).text().split( '</response_data>' )[0],
response_json = $.parseJSON( response_data ),
error_msgs = '',
error_item = '',
error_msg_wrap = $( '<div />', {
'class': 'ps-signup-errors'
} ),
has_error = false,
main_wrap = $( '#prosites-signup-form-checkout' );
if ( ! response_json.blog_title_available ) {
has_error = true;
error_item = $( '<div />',{
'class' : 'error_item blog_title'
} );
error_item.html( 'The blog title is not available' );
error_msg_wrap.append( error_item );
}
if ( ! response_json.blogname_available ) {
has_error = true;
error_item = $( '<div />',{
'class' : 'error_item blogname_available'
} );
error_item.html( 'The blog name is not available' );
error_msg_wrap.append( error_item );
}
if ( ! response_json.email_available ) {
has_error = true;
error_item = $( '<div />',{
'class' : 'error_item email_available'
} );
error_item.html( 'The user email is not available' );
error_msg_wrap.append( error_item );
}
if ( ! response_json.username_available ) {
has_error = true;
error_item = $( '<div />',{
'class' : 'error_item username_available'
} );
error_item.html( 'The username is not available' );
error_msg_wrap.append( error_item );
}
if ( has_error ) {
main_wrap.prepend( error_msg_wrap );
}
}
}
$( document ).ready(function(){
/*$( '#prosites-user-register div.username,#prosites-user-register div.email,\
#prosites-user-register div.blogname,\
#prosites-user-register div.blog_title\
.gform_footer input[type=submit]'
).remove();*/
WPMUDEV_PS_Custom_Checkout_Fields.load();
});
})(jQuery)
</script>
<?php
}
public function site_fields() {
if ( empty( self::$site_fields ) ) {
return;
}
$out = '';
foreach ( self::$site_fields as $field_key => $field_data ) {
$out .= $this->field_builder( $field_key, $field_data );
}
echo $out;
}
public function user_fields() {
if ( empty( self::$user_fields ) ) {
return;
}
$out = '';
foreach ( self::$user_fields as $field_key => $field_data ) {
$out .= $this->field_builder( $field_key, $field_data );
}
echo $out;
}
public function field_builder( $field_key, $field_data ) {
if ( ! isset( $field_data['type'] ) ) {
return '';
}
$out = '';
switch( $field_data['type'] ) {
case 'raw_html' : $out = $field_data[ 'value' ]; break;
case 'text' : $out = $this->text_field( $field_key, $field_data ); break;
case 'select' : $out = $this->select_field( $field_key, $field_data ); break;
case 'checkbox' : $out = $this->checkbox_field( $field_key, $field_data ); break;
case 'shortcode' : $out = $this->shortcake_field( $field_key, $field_data ); break;
}
return $out;
}
protected function shortcake_field( $field_key, $field_data ) {
return do_shortcode( $field_data[ 'value' ] );
}
protected function text_field( $field_key, $field_data ) {
$wrapper_class = isset( $field_data['wrapper_class'] ) ? $field_data['wrapper_class'] : '';
$class = isset( $field_data['class'] ) ? $field_data['class'] : '';
$value = isset( $field_data['value'] ) ? $field_data['value'] : '';
$label_tile = isset( $field_data['label_tile'] ) ? $field_data['label_tile'] : '';
$label = '';
if ( ! empty( $label_tile ) ) {
$label = "<label>{$label_tile}</label>";
}
return "
<div class=\"{$wrapper_class}\">
{$label}
<input type=\"text\" class=\"{$class}\" name=\"{$field_key}\" value=\"{$value}\" />
</div>
";
}
protected function select_field( $field_key, $field_data ) {
$wrapper_class = isset( $field_data['wrapper_class'] ) ? $field_data['wrapper_class'] : '';
$class = isset( $field_data['class'] ) ? $field_data['class'] : '';
$value = isset( $field_data['value'] ) ? $field_data['value'] : '';
$options = '';
$label_tile = isset( $field_data['label_tile'] ) ? $field_data['label_tile'] : '';
$label = '';
if ( isset( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
foreach ( $field_data['options'] as $key => $option ) {
$selected = selected( $value, $key );
$options .= "<option value=\"{$key}\" name=\"{$field_key}\" {$selected} >{$option}</option>";
}
} else {
return '';
}
if ( ! empty( $label_tile ) ) {
$label = "<label>{$label_tile}</label>";
}
return "
<div class=\"{$wrapper_class}\">
{$label}
<select class=\"{$class}\" name=\"{$field_key}\">
{$options}
<select>
</div>
";
}
protected function checkbox_field( $field_key, $field_data ) {
$wrapper_class = isset( $field_data['wrapper_class'] ) ? $field_data['wrapper_class'] : '';
$class = isset( $field_data['class'] ) ? $field_data['class'] : '';
$value = isset( $field_data['value'] ) ? $field_data['value'] : '';
$checkboxes = '';
$label_tile = isset( $field_data['label_tile'] ) ? $field_data['label_tile'] : '';
$label = '';
if ( ! empty( $label_tile ) ) {
$label = "<label>{$label_tile}</label>";
}
if ( isset( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
foreach ( $field_data['options'] as $checkbox_value => $checkbox_label ) {
$selected = selected( $value, $checkbox_value );
$checkboxes .= '<div>';
$checkboxes .= "<label><input type=\"checkbox\" name=\"{$field_key}\" value=\"$checkbox_value\" {$selected} /> {$checkbox_label}</label>";
$checkboxes .= '</div>';
}
} else {
return '';
}
return "
<div class=\"{$wrapper_class}\">
<div>{$label}</div>
{$checkboxes}
</div>
";
}
}
if ( ! function_exists( 'wpmudev_ps_custom_checkout_fields' ) ) {
function wpmudev_ps_custom_checkout_fields(){
return WPMUDEV_PS_Custom_Checkout_Fields::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_ps_custom_checkout_fields', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment