dimanche 29 mars 2015

Wordpress starter plugin prevent save to data base after refresh browser

HI this is my first post and the main question is about preventing my script to save post data in database after refreshing browser but I would like to write plugin for WordPress to use for every one as a starter script. So my second question is if this script is correct written and how would you do it.



<?php
/*
Plugin Name: Yoolek Starter Plugin
Plugin URI: http://yoolek.com/
Description: Yoolek Starter Plugin - simple starter plugin to use with shortcode [yoolek-starter-plugin]
Version: 1.0
Author:
Author URI: http://yoolek.com/
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );

if ( ! class_exists( 'YoolekStarterPlugin' ) )
{
class YoolekStarterPlugin {



protected $tag = 'yoolek-starter-plugin';

protected $yoo_name;
protected $yoo_year;

protected $admin_option1;
protected $admin_option2;

public function __construct()
{
/* we will use shortcode [yoolek-starter-plugin] in post or page */
add_shortcode( $this->tag, array($this, 'run_yoolek' ) );

add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}



public function run_yoolek()
{
ob_start();/*to keep content correctly (not under shortcode) look down <?php return ob_get_clean();?>*/


$this->create_form();
$this->result();
return ob_get_clean();
}


/**********************************************************/
private function result(){
$this->admin_option1 = get_option("yoolek-starter-plugin-option1");/*admin option*/
$this->admin_option2 = get_option("yoolek-starter-plugin-option2");/*admin option*/
echo "admin option 1= ".$this->admin_option1."<br/>";
echo "admin option 2= ".$this->admin_option2."<br/>";
global $wpdb; /* save data to database and display*/
$table_name = $wpdb->prefix . 'yoolek_starter_plugin';


if (isset($_POST['yoo_submit'])) {
$this->yoo_name = $_POST["yoo_name"];/* option*/
$this->yoo_year = $_POST["yoo_year"];/* option*/
$wpdb->insert($table_name, array(
"time" => date("Y-m-d H:i:s"),
"yoo_name" => $this->yoo_name,
"yoo_year" => $this->yoo_year
));
}
$result = $wpdb->get_results("SELECT * FROM ".$table_name." ORDER BY time DESC");

echo '<table class="table table-striped table-bordered table-condensed">';
echo '<thead>';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Time</th>';
echo '<th>Name</th>';
echo '<th>Year</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach($result as $row){
echo '<tr>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->time.'</td>';
echo '<td>'.$row->yoo_name.'</td>';
echo '<td>'.$row->yoo_year.'</td>';
echo '</tr>';
};
echo '</tbody>';
echo '</table>';

}
/**********************************************************/

/**********************************************************/
private function create_form()
{
?><form class="form-horizontal" action="" method="post">
<fieldset>


<legend>Yoolek Starter Plugin Form</legend>

<!-- Select Basic -->
<br><br>

<!-- Text input-->
<div class="form-group">
<label class="col-xs-5 col-sm-3 control-label" for="yoo_name">Name:</label>
<div class="col-xs-7 col-sm-7">
<input id="yoo-name" name="yoo_name" placeholder="your name" class="form-control input-md" required="" type="text" value="">

</div>
</div>
<div class="form-group">
<label class="col-xs-5 col-sm-3 control-label" for="yoo-year">Your lucky year:</label>
<div class="col-xs-7 col-sm-7">
<select id="yoo-year" name="yoo_year" class="form-control">
<option value="1991">1991</option>
<option value="1992" selected>1992</option>
<option value="1993">1993</option>
</select>
</div>
</div>


<!-- Button -->
<div class="form-group">
<label class="col-xs-5 col-sm-4 control-label" for="submit"></label>
<div class="col-xs-7 col-sm-6">
<button id="submit" name="yoo_submit" class="btn btn-block btn-success">Submit</button>
</div>
</div>

</fieldset>
</form>
<br><br>
<?php



}/*html_form*/


/**********************************************************/
/*ADMIN,PLUGIN HOOKS AND FUNCTIONS*/
function settings_page() {


/*update*/
if (isset($_POST['option_update'])) {
check_admin_referer();//Tests if the current request was referred from an admin page
$variable1 = $_POST['field_option1'];
update_option("yoolek-starter-plugin-option1", $variable1);//update option
$variable2 = $_POST['field_option2'];
update_option("yoolek-starter-plugin-option2", $variable2);//update option
echo "<div class='updated'><p><strong>Yoolek Starter Plugin has been updated</strong></p></div>";
}

/**/
?><form method="post" action=" <?php admin_url('options-general.php?page=yoolek-starter-plugin.php')?> ">
<div class="wrap">
<h2>YOOLEK STARTER PLUGIN SETTINGS<sup style='color:#D54E21;font-size:12px;'><?php //echo $yoolek_starter_plugin_version; ?></sup></h2>
<div>
<label for="yoo_option1" style="width:200px; line-height:23px; float:left;">option1</label>
<select name="field_option1" id="option1" width="200" style="width: 200px">
<option value="option nr1" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr1") { ?>selected="true" <?php }; ?>>option nr1</option>
<option value="option nr2" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr2") { ?>selected="true" <?php }; ?>>option nr2</option>
<option value="option nr3" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr3") { ?>selected="true" <?php }; ?>>option nr3</option>
</select>
<span class="setting-description"> Select option1.</span>
</div>
<div>
<label for="yoo_option2" style="width:200px; line-height:23px; float:left;">option2</label>
<input type="text" width="200" style="width: 200px" size="16" maxlength="12" name="field_option2" id="option2" value="<?php echo get_option("yoolek-starter-plugin-option2"); ?>" />
<span class="setting-description"> Enter option2.</span>
</div>
<p class="submit">
<input name="option_update" value="Save Changes" type="submit" class="button-primary" />
</p>


</div>
</form><?php
}
function admin_menu () {
add_options_page( 'Yoolek Starter Plugin Page', 'Yoolek Starter Plugin Menu', 'manage_options', 'yoolek-starter-plugin', array( $this, 'settings_page' ) );
}


static function plugin_activation() {

/* create table in data base*/
global $wpdb;
$table_name = $wpdb->prefix . 'yoolek_starter_plugin';/* name z _*/
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
yoo_name tinytext NOT NULL,
yoo_year text NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

/*create_options*/
/*add_option( $option, $value, $deprecated, $autoload );*/
add_option('yoolek-starter-plugin-option1', 'option1Value1', '', 'yes');/*<----------------------------------- Creates new database field */
add_option('yoolek-starter-plugin-option2', 'option1Value2', '', 'yes');/*<----------------------------------- Creates new database field */
}

static function plugin_deactivation() {

global $wpdb;
$table = $wpdb->prefix . 'yoolek_starter_plugin';
$wpdb->query("DROP TABLE IF EXISTS $table");

delete_option('yoolek-starter-plugin-option1');/*<----------------------------------------------------------- Deletes the database field */
delete_option('yoolek-starter-plugin-option2');/*<----------------------------------------------------------- Deletes the database field */
}

}/*class*/
new YoolekStarterPlugin;

register_activation_hook( __FILE__, array( 'YoolekStarterPlugin', 'plugin_activation' ) );/*<------------------------------ Runs when plugin is activated */
register_deactivation_hook( __FILE__, array( 'YoolekStarterPlugin', 'plugin_deactivation' ) );/*<---------------------------- Runs on plugin deactivation */
}/*if*/?>

Aucun commentaire:

Enregistrer un commentaire