All pastes #251292 Raw Edit

Unnamed

public php v1 · immutable
#251292 ·published 2006-11-17 23:45 UTC
rendered paste body
/*	U.S. States Add-On for WP Contact Form Plugin	*********************************************			<?php get_states() ?> 			returns an array of states. 			usage:  arr->state_abbr			        arr->state_name*//*************************************************************Declare and name the table we'll be adding to the dB*************************************************************/$wpdb->us_states = 'us_states';/*************************************************************This functions lists options for a <select> statement INPUT: 2-Dimensional Array where each option has a value element and a display elementOUTPUT: <option value=arr[0][0]>arr[0][1]</option>NOTE: If array is only one dimension function will output <option>arr[0]</option>*************************************************************/function list_options($arr_options) {    $output = array();    foreach($arr_options as $arr_option) {        if(is_array($arr_option))            $output[] = '<option value="' . $arr_option[0] . '">' . $arr_option[1] . '</option>';        else            $output[] = '<option>' . $arr_option . '</option>';    }    return implode($output);}/*************************************************************This functions calls the states from the dB INPUT: OUTPUT: 2-Dimensional Array where each element stores the state's abbreviation and nameNOTE:*************************************************************/    function get_states() {	global $wpdb;    return $wpdb->get_results("SELECT * FROM $wpdb->us_states ORDER BY state_name",ARRAY_N);}/*************************************************************This functions creates a table to store the states in the dB and then calls populate_states()INPUT: OUTPUT: NOTE: If table is already built the script will die with an error*************************************************************/function build_states_table() {    global $wpdb;        //check to see if table already exists, if it does kill the script    if($tables = $wpdb->get_col('SHOW TABLES;')) {		// For every table in the database		foreach($tables as $table) {			// If a table named 'us_states' already exists in the dB			if (strtolower($table) == 'us_states') {			    die(__('WTF are you trying to do? Blow up the dB?', 'wpcf'));            }        }    }            //define the SQL code we'll use to create the table    $us_cqueries="CREATE TABLE $wpdb->us_states (      state_abbr char(2) NOT NULL, state_name char (25) NOT NULL,      PRIMARY KEY (state_abbr)    )";        $wpdb->query($us_cqueries);        populate_states();       }/*************************************************************This functions populates the us_states table in the dB with each state's abbreviation and nameINPUT:OUTPUT:NOTE: *************************************************************/function populate_states() {    global $wpdb;        //define the SQL code we'll use to add the states to the table    $us_aqueries="INSERT INTO $wpdb->us_states (state_abbr,state_name)      values('AL','Alabama'),('AK','Alaska'),('AZ','Arizona'),('AR','Arkansas'),('CA','California'),('CO','Colorado'),('CT','Connecticut'),      ('DE','Delaware'),('DC','District of Columbia'),('FL','Florida'),('GA','Georgia'),('HI','Hawaii'),('ID','Idaho'),('IL','Illinois'),      ('IN','Indiana'),('IA','Iowa'),('KS','Kansas'),('KY','Kentucky'),('LA','Louisiana'),('ME','Maine'),('MD','Maryland'),('MA','Massachusettes'),      ('MI','Michigan'),('MN','Minnesota'),('MS','Mississippi'),('MO','Missouri'),('MT','Montana'),('NE','Nebraska'),('NV','Nevada'),      ('NH','New Hampshire'),('NJ','New Jersey'),('NM','New Mexico'),('NY','New York'),('NC','North Carolina'),('ND','North Dakota'),      ('OH','Ohio'),('OK','Oklahoma'),('OR','Oregon'),('PA','Pennsylvania'),('RI','Rhode Island'),('SC','South Carolina'),('SD','South Dakota'),      ('TN','Tennessee'),('TX','Texas'),('UT','Utah'),('VT','Vermont'),('VA','Virginia'),('WA','Washington'),('WV','West Virginia'),      ('WI','Wisconsin'),('WY','Wyoming')";    $wpdb->query($us_aqueries);}/*************************************************************This functions removes the us_states table which stores the states in the dBINPUT:OUTPUT:NOTE: If table doesn't exist the script will die with an error*************************************************************///Uninstall function removes states dBfunction remove_states_table() {    global $wpdb;        //check to see if table already exists, if it doesn't kill the script    if($tables = $wpdb->get_col('SHOW TABLES;')) {		// For every table in the database		foreach($tables as $table) {			// If a table named 'us_states' already exists in the dB			if (strtolower($table) == 'us_states') {		        //define the SQL code we'll use to remove the table                $us_cqueries="DROP TABLE $wpdb->us_states";                $wpdb->query($us_cqueries);                return true;            }        }        die(__('WTF are you trying to do? That table is not installed!', 'wpcf'));    }      }add_action('admin_head', 'wpcf_add_options_page');add_filter('wp_head', 'wpcf_css');add_filter('the_content', 'wpcf_callback', 7);add_filter('activate_wp-contact-form/wp-contactform.php', 'build_states_table');add_filter('deactivate_wp-contact-form/wp-contactform.php', 'remove_states_table');