| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: WPDB Float Test |
|---|
| 4 | Plugin URI: http://core.trac.wordpress.org/ticket/18407 |
|---|
| 5 | Description: Tests use of %f in wpdb queries. |
|---|
| 6 | Author: dllh |
|---|
| 7 | Version: 0.1 |
|---|
| 8 | Author URI: http://profiles.wordpress.org/users/dllh |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | class WPDB_Float_Test { |
|---|
| 12 | |
|---|
| 13 | function activate() { |
|---|
| 14 | global $wpdb; |
|---|
| 15 | |
|---|
| 16 | $sql = "CREATE TABLE {$wpdb->prefix}float_test ( |
|---|
| 17 | id int(10) unsigned not null primary key auto_increment, |
|---|
| 18 | entered varchar(100), |
|---|
| 19 | num float( 10, 4 ) |
|---|
| 20 | );"; |
|---|
| 21 | |
|---|
| 22 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|---|
| 23 | dbDelta( $sql ); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | function deactivate() { |
|---|
| 27 | global $wpdb; |
|---|
| 28 | $wpdb->query( "DROP TABLE {$wpdb->prefix}float_test" ); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | function menu() { |
|---|
| 32 | add_management_page('WPDB Float Test', 'WPDB Float Test', 'manage_options', 'wpdb-float-test-id', array( 'WPDB_Float_Test', 'options' ) ); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | function options() { |
|---|
| 36 | global $wpdb; |
|---|
| 37 | |
|---|
| 38 | if (!current_user_can('manage_options')) { |
|---|
| 39 | wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | print "<h2>WPDB Float Test</h2>\n"; |
|---|
| 43 | print "<form method='post'>\n"; |
|---|
| 44 | print "<input id='float' name='float' type='text' value='Enter a float' onfocus='this.value=\"\"' /><br />\n"; |
|---|
| 45 | print "<input type='submit' value='Add' />\n"; |
|---|
| 46 | print "</form>\n"; |
|---|
| 47 | |
|---|
| 48 | if( isset( $_POST['float'] ) ) { |
|---|
| 49 | $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->prefix}float_test ( entered, num ) VALUES ( %s, %f )", $_POST['float'], $_POST['float'] ) ); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | print "<h3>The Last 10 Floats Inserted</h3>"; |
|---|
| 53 | |
|---|
| 54 | print "<table cellpadding='5' cellspacing='5'>\n"; |
|---|
| 55 | print "<tr><th>Value Entered</th><th>Float Value</th></tr>\n"; |
|---|
| 56 | foreach( $wpdb->get_results( "SELECT entered, num FROM {$wpdb->prefix}float_test ORDER BY id DESC LIMIT 10" ) as $row ) { |
|---|
| 57 | print "<tr><td>" . esc_html( $row->entered ) . "</td><td aligh='right'>{$row->num}</td></tr>"; |
|---|
| 58 | } |
|---|
| 59 | print "</table>\n"; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | register_activation_hook( __FILE__, array( 'WPDB_Float_Test', 'activate' ) ); |
|---|
| 65 | register_deactivation_hook( __FILE__, array( 'WPDB_Float_Test', 'deactivate' ) ); |
|---|
| 66 | add_action('admin_menu', array( 'WPDB_Float_Test', 'menu' ) ); |
|---|
| 67 | |
|---|