Forked from pippinsplugins/gist:ececad5d57e3946c5af3
Created
December 21, 2016 18:25
-
-
Save Basilakis/c93e0cfc5842f2193fd8825ba712f017 to your computer and use it in GitHub Desktop.
Adds billing address fields to Restrict Content Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Restrict Content Pro - Collect Billing Address | |
* Description: Collect customers billing address during registration through Restrict Content Pro | |
* Author: Pippin Williamson | |
* Version: 1.0 | |
*/ | |
class RCP_Billing_Address { | |
public function __construct() { | |
add_action( 'rcp_after_password_registration_field', array( $this, 'fields' ) ); | |
add_action( 'rcp_form_errors', array( $this, 'error_checks' ) ); | |
add_filter( 'rcp_subscription_data', array( $this, 'subscription_data' ) ); | |
add_filter( 'rcp_paypal_args', array( $this, 'paypal_args' ), 10, 2 ); | |
add_action( 'rcp_view_member_after', array( $this, 'member_details' )); | |
} | |
public function fields() { | |
?> | |
<div id="rcp_user_address_fields"> | |
<p id="rcp_street"> | |
<label for="rcp_street"><?php _e( 'Address Line 1', 'rcp' ); ?></label> | |
<input name="rcp_street" id="rcp_street" class="required" type="text" <?php if( isset( $_POST['rcp_street'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_street'] ) . '"'; } ?>/> | |
</p> | |
<p id="rcp_street_2"> | |
<label for="rcp_street_2"><?php _e( 'Address Line 2', 'rcp' ); ?></label> | |
<input name="rcp_street_2" id="rcp_street_2" type="text" <?php if( isset( $_POST['rcp_street_2'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_street_2'] ) . '"'; } ?>/> | |
</p> | |
<p id="rcp_city"> | |
<label for="rcp_city"><?php _e( 'City', 'rcp' ); ?></label> | |
<input name="rcp_city" id="rcp_city" class="required" type="text" <?php if( isset( $_POST['rcp_city'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_city'] ) . '"'; } ?>/> | |
</p> | |
<p id="rcp_country"> | |
<label for="rcp_country"><?php _e( 'Country', 'rcp' ); ?></label> | |
<select name="rcp_country" id="rcp_country"> | |
<?php foreach( $this->get_countries() as $key => $country ) : ?> | |
<option value="<?php echo $key; ?>"><?php echo $country; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</p> | |
<p id="rcp_state"> | |
<label for="rcp_state"><?php _e( 'State / Province', 'rcp' ); ?></label> | |
<input name="rcp_state" id="rcp_state" class="required" type="text" <?php if( isset( $_POST['rcp_state'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_state'] ) . '"'; } ?>/> | |
</p> | |
<p id="rcp_zip"> | |
<label for="rcp_zip"><?php _e( 'Zip / Postal Code', 'rcp' ); ?></label> | |
<input name="rcp_zip" id="rcp_zip" class="required" type="text" <?php if( isset( $_POST['rcp_zip'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_zip'] ) . '"'; } ?>/> | |
</p> | |
</div> | |
<?php | |
} | |
public function error_checks( $data ) { | |
if( empty( $data['rcp_street'] ) ) { | |
rcp_errors()->add( 'empty_address', __( 'Please enter your address', 'rcp' ), 'register' ); | |
} | |
if( empty( $data['rcp_city'] ) ) { | |
rcp_errors()->add( 'empty_city', __( 'Please enter your city', 'rcp' ), 'register' ); | |
} | |
if( empty( $data['rcp_state'] ) ) { | |
rcp_errors()->add( 'empty_state', __( 'Please enter your state', 'rcp' ), 'register' ); | |
} | |
if( empty( $data['rcp_country'] ) || $data['rcp_country'] == '*' ) { | |
rcp_errors()->add( 'empty_country', __( 'Please select your country', 'rcp' ), 'register' ); | |
} | |
if( empty( $data['rcp_zip'] ) ) { | |
rcp_errors()->add( 'empty_zip', __( 'Please enter your zip code', 'rcp' ), 'register' ); | |
} | |
} | |
public function subscription_data( $subscription_data ) { | |
$subscription_data['address'] = array(); | |
$subscription_data['address']['line1'] = sanitize_text_field( $_POST['rcp_street'] ); | |
$subscription_data['address']['line2'] = isset( $_POST['rcp_street_2'] ) ? sanitize_text_field( $_POST['rcp_street_2'] ) : ''; | |
$subscription_data['address']['line2'] = sanitize_text_field( $_POST['rcp_city'] ); | |
$subscription_data['address']['state'] = sanitize_text_field( $_POST['rcp_state'] ); | |
$subscription_data['address']['zip'] = sanitize_text_field( $_POST['rcp_zip'] ); | |
update_user_meta( $subscription_data['user_id'], 'rcp_address', $subscription_data['address'] ); | |
return $subscription_data; | |
} | |
public function paypal_args( $args, $subscription_data ) { | |
unset( $args['no_shipping'] ); | |
unset( $args['tax'] ); | |
$args['address1'] = $subscription_data['address']['line1']; | |
$args['address2'] = $subscription_data['address']['line2']; | |
$args['city'] = $subscription_data['address']['city']; | |
$args['state'] = $subscription_data['address']['state']; | |
$args['zip'] = $subscription_data['address']['zip']; | |
$args['country'] = 'US'; | |
return $args; | |
} | |
public function member_details( $user_id ) { | |
$address = get_user_meta( $user_id, 'rcp_address', true ); | |
if( empty( $address ) ) | |
return; | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"> | |
<?php _e( 'Address', 'rcp' ); ?> | |
</th> | |
<td> | |
<?php foreach( $address as $line ) : ?> | |
<?php echo $line . '<br/>'; ?> | |
<?php endforeach; ?> | |
</td> | |
</tr> | |
<?php | |
} | |
public function get_countries() { | |
$countries = array( | |
'*' => __( 'Choose', 'rcp' ), | |
'US' => 'United States', | |
'CA' => 'Canada', | |
'GB' => 'United Kingdom', | |
'AF' => 'Afghanistan', | |
'AL' => 'Albania', | |
'DZ' => 'Algeria', | |
'AS' => 'American Samoa', | |
'AD' => 'Andorra', | |
'AO' => 'Angola', | |
'AI' => 'Anguilla', | |
'AQ' => 'Antarctica', | |
'AG' => 'Antigua and Barbuda', | |
'AR' => 'Argentina', | |
'AM' => 'Armenia', | |
'AW' => 'Aruba', | |
'AU' => 'Australia', | |
'AT' => 'Austria', | |
'AZ' => 'Azerbaijan', | |
'BS' => 'Bahamas', | |
'BH' => 'Bahrain', | |
'BD' => 'Bangladesh', | |
'BB' => 'Barbados', | |
'BY' => 'Belarus', | |
'BE' => 'Belgium', | |
'BZ' => 'Belize', | |
'BJ' => 'Benin', | |
'BM' => 'Bermuda', | |
'BT' => 'Bhutan', | |
'BO' => 'Bolivia', | |
'BA' => 'Bosnia and Herzegovina', | |
'BW' => 'Botswana', | |
'BV' => 'Bouvet Island', | |
'BR' => 'Brazil', | |
'IO' => 'British Indian Ocean Territory', | |
'BN' => 'Brunei Darrussalam', | |
'BG' => 'Bulgaria', | |
'BF' => 'Burkina Faso', | |
'BI' => 'Burundi', | |
'KH' => 'Cambodia', | |
'CM' => 'Cameroon', | |
'CV' => 'Cape Verde', | |
'KY' => 'Cayman Islands', | |
'CF' => 'Central African Republic', | |
'TD' => 'Chad', | |
'CL' => 'Chile', | |
'CN' => 'China', | |
'CX' => 'Christmas Island', | |
'CC' => 'Cocos Islands', | |
'CO' => 'Colombia', | |
'KM' => 'Comoros', | |
'CD' => 'Congo, Democratic People\'s Republic', | |
'CG' => 'Congo, Republic of', | |
'CK' => 'Cook Islands', | |
'CR' => 'Costa Rica', | |
'CI' => 'Cote d\'Ivoire', | |
'HR' => 'Croatia/Hrvatska', | |
'CU' => 'Cuba', | |
'CY' => 'Cyprus Island', | |
'CZ' => 'Czech Republic', | |
'DK' => 'Denmark', | |
'DJ' => 'Djibouti', | |
'DM' => 'Dominica', | |
'DO' => 'Dominican Republic', | |
'TP' => 'East Timor', | |
'EC' => 'Ecuador', | |
'EG' => 'Egypt', | |
'GQ' => 'Equatorial Guinea', | |
'SV' => 'El Salvador', | |
'ER' => 'Eritrea', | |
'EE' => 'Estonia', | |
'ET' => 'Ethiopia', | |
'FK' => 'Falkland Islands', | |
'FO' => 'Faroe Islands', | |
'FJ' => 'Fiji', | |
'FI' => 'Finland', | |
'FR' => 'France', | |
'GF' => 'French Guiana', | |
'PF' => 'French Polynesia', | |
'TF' => 'French Southern Territories', | |
'GA' => 'Gabon', | |
'GM' => 'Gambia', | |
'GE' => 'Georgia', | |
'DE' => 'Germany', | |
'GR' => 'Greece', | |
'GH' => 'Ghana', | |
'GI' => 'Gibraltar', | |
'GL' => 'Greenland', | |
'GD' => 'Grenada', | |
'GP' => 'Guadeloupe', | |
'GU' => 'Guam', | |
'GT' => 'Guatemala', | |
'GG' => 'Guernsey', | |
'GN' => 'Guinea', | |
'GW' => 'Guinea-Bissau', | |
'GY' => 'Guyana', | |
'HT' => 'Haiti', | |
'HM' => 'Heard and McDonald Islands', | |
'VA' => 'Holy See (City Vatican State)', | |
'HN' => 'Honduras', | |
'HK' => 'Hong Kong', | |
'HU' => 'Hungary', | |
'IS' => 'Iceland', | |
'IN' => 'India', | |
'ID' => 'Indonesia', | |
'IR' => 'Iran', | |
'IQ' => 'Iraq', | |
'IE' => 'Ireland', | |
'IM' => 'Isle of Man', | |
'IL' => 'Israel', | |
'IT' => 'Italy', | |
'JM' => 'Jamaica', | |
'JP' => 'Japan', | |
'JE' => 'Jersey', | |
'JO' => 'Jordan', | |
'KZ' => 'Kazakhstan', | |
'KE' => 'Kenya', | |
'KI' => 'Kiribati', | |
'KW' => 'Kuwait', | |
'KG' => 'Kyrgyzstan', | |
'LA' => 'Lao People\'s Democratic Republic', | |
'LV' => 'Latvia', | |
'LB' => 'Lebanon', | |
'LS' => 'Lesotho', | |
'LR' => 'Liberia', | |
'LY' => 'Libyan Arab Jamahiriya', | |
'LI' => 'Liechtenstein', | |
'LT' => 'Lithuania', | |
'LU' => 'Luxembourgh', | |
'MO' => 'Macau', | |
'MK' => 'Macedonia', | |
'MG' => 'Madagascar', | |
'MW' => 'Malawi', | |
'MY' => 'Malaysia', | |
'Mv' => 'Maldives', | |
'ML' => 'Mali', | |
'MT' => 'Malta', | |
'MH' => 'Marshall Islands', | |
'MQ' => 'Martinique', | |
'MR' => 'Mauritania', | |
'MU' => 'Mauritius', | |
'YT' => 'Mayotte', | |
'MX' => 'Mexico', | |
'FM' => 'Micronesia', | |
'MD' => 'Moldova, Republic of', | |
'MC' => 'Monaco', | |
'MN' => 'Mongolia', | |
'ME' => 'Montenegro', | |
'MS' => 'Montserrat', | |
'MA' => 'Morocco', | |
'MZ' => 'Mozambique', | |
'MM' => 'Myanmar', | |
'NA' => 'Namibia', | |
'NR' => 'Nauru', | |
'NP' => 'Nepal', | |
'NL' => 'Netherlands', | |
'AN' => 'Netherlands Antilles', | |
'NC' => 'New Caledonia', | |
'NZ' => 'New Zealand', | |
'NI' => 'Nicaragua', | |
'NE' => 'Niger', | |
'NG' => 'Nigeria', | |
'NU' => 'Niue', | |
'NF' => 'Norfolk Island', | |
'KR' => 'North Korea', | |
'MP' => 'Northern Mariana Islands', | |
'NO' => 'Norway', | |
'OM' => 'Oman', | |
'PK' => 'Pakistan', | |
'PW' => 'Palau', | |
'PS' => 'Palestinian Territories', | |
'PA' => 'Panama', | |
'PG' => 'Papua New Guinea', | |
'PY' => 'Paraguay', | |
'PE' => 'Peru', | |
'PH' => 'Phillipines', | |
'PN' => 'Pitcairn Island', | |
'PL' => 'Poland', | |
'PT' => 'Portugal', | |
'PR' => 'Puerto Rico', | |
'QA' => 'Qatar', | |
'RE' => 'Reunion Island', | |
'RO' => 'Romania', | |
'RU' => 'Russian Federation', | |
'RW' => 'Rwanda', | |
'SH' => 'Saint Helena', | |
'KN' => 'Saint Kitts and Nevis', | |
'LC' => 'Saint Lucia', | |
'PM' => 'Saint Pierre and Miquelon', | |
'VC' => 'Saint Vincent and the Grenadines', | |
'SM' => 'San Marino', | |
'ST' => 'Sao Tome and Principe', | |
'SA' => 'Saudi Arabia', | |
'SN' => 'Senegal', | |
'RS' => 'Serbia', | |
'SC' => 'Seychelles', | |
'SL' => 'Sierra Leone', | |
'SG' => 'Singapore', | |
'SK' => 'Slovak Republic', | |
'SI' => 'Slovenia', | |
'SB' => 'Solomon Islands', | |
'SO' => 'Somalia', | |
'ZA' => 'South Africa', | |
'GS' => 'South Georgia', | |
'KP' => 'South Korea', | |
'ES' => 'Spain', | |
'LK' => 'Sri Lanka', | |
'SD' => 'Sudan', | |
'SR' => 'Suriname', | |
'SJ' => 'Svalbard and Jan Mayen Islands', | |
'SZ' => 'Swaziland', | |
'SE' => 'Sweden', | |
'CH' => 'Switzerland', | |
'SY' => 'Syrian Arab Republic', | |
'TW' => 'Taiwan', | |
'TJ' => 'Tajikistan', | |
'TZ' => 'Tanzania', | |
'TG' => 'Togo', | |
'TK' => 'Tokelau', | |
'TO' => 'Tonga', | |
'TH' => 'Thailand', | |
'TT' => 'Trinidad and Tobago', | |
'TN' => 'Tunisia', | |
'TR' => 'Turkey', | |
'TM' => 'Turkmenistan', | |
'TC' => 'Turks and Caicos Islands', | |
'TV' => 'Tuvalu', | |
'UG' => 'Uganda', | |
'UA' => 'Ukraine', | |
'AE' => 'United Arab Emirates', | |
'UY' => 'Uruguay', | |
'UM' => 'US Minor Outlying Islands', | |
'UZ' => 'Uzbekistan', | |
'VU' => 'Vanuatu', | |
'VE' => 'Venezuela', | |
'VN' => 'Vietnam', | |
'VG' => 'Virgin Islands (British)', | |
'VI' => 'Virgin Islands (USA)', | |
'WF' => 'Wallis and Futuna Islands', | |
'EH' => 'Western Sahara', | |
'WS' => 'Western Samoa', | |
'YE' => 'Yemen', | |
'YU' => 'Yugoslavia', | |
'ZM' => 'Zambia', | |
'ZW' => 'Zimbabwe' | |
); | |
return $countries; | |
} | |
} | |
new RCP_Billing_Address; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment