Skip to content

Instantly share code, notes, and snippets.

@luisabarca
Created December 17, 2011 18:32
Show Gist options
  • Save luisabarca/1490997 to your computer and use it in GitHub Desktop.
Save luisabarca/1490997 to your computer and use it in GitHub Desktop.
Bienes raices Test Plugin
<p>
<label for="m2">Metros cuadrados</label>
<input type="text" id="m2" name="m2" value="<?php echo $m2 ?>" />
</p>
<p>
<label for="construido">Superficie construida</label>
<input type="text" id="construido" name="construido" value="<?php echo $construido ?>" />
</p>
<?php
/*
Plugin Name: Bienes Raices
Description: Mi plugin de bienes raices del curso WP3
Version: 1.0
Author: Luis Abarca
Author URI: http://luisabarca.com
Plugin URI: http://justoalblanco.com/wordpress/bienesraices
License: http://justoalblanco.com/wordpress/bienesraices/license
*/
global $wpdb;
// Agrego el nombre de mi tabla al objeto $wpdb
$wpdb->realty = $wpdb->prefix . 'realty';
// incluir funciones de instalacion y desinstalacion
add_action('init', 'jab_br_custom_type');
function jab_br_custom_type()
{
$args = array(
'label' => 'Bienes Raices',
'has_archive' => true,
'public' => true,
'supports' => array('title', 'editor', 'custom-fields', 'thumbnails')
);
register_post_type('jab_bienes', $args);
}
// funciones exclusivas de la administracion del sitio
if ( is_admin() ) {
// en el evento agregar metaboxes de WP
add_action('add_meta_boxes', 'jab_br_metaboxes');
// agregamos un metabox en el costado
function jab_br_metaboxes()
{
if ( current_user_can('manage_options') ) {
add_meta_box('jab_br_mb-1', 'Datos de la propiedad', 'jab_br_mb_callback', 'jab_bienes', 'side');
}
remove_meta_box('postcustom', 'jab_bienes', 'normal');
}
// pinta el meta box
function jab_br_mb_callback()
{
global $post;
$post_id = $post->ID;
$m2 = esc_attr(get_post_meta($post_id, 'm2', true));
$construido = get_post_meta($post_id, '_construido', true);
include 'form.php';
wp_nonce_field(__FILE__, 'jab_br_mb');
}
// al aguardar el post, guardar los datos del Metabox
add_action('save_post', 'jab_br_save_post');
function jab_br_save_post($post_id)
{
global $wpdb;
global $post;
// checar si la fuente es fiable
if ( !wp_verify_nonce($_POST['jab_br_mb'], __FILE__) ) {
return $post_id;
}
if ( $post->post_type != 'jab_bienes') {
return false;
}
// Si es un borrador
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
// checa si el usuario puede editar la entrada actual
if ( !current_user_can('edit_post')
|| !current_user_can('edit_post', $post_id) ) {
return false;
}
// TODO: guardar los datos del mb
extract($_POST);
$action = 'update_post_meta';
// Usando custom fields (post meta)
if ( isset($m2) && empty($m2) ) {
$action = 'delete_post_meta';
}
$action($post_id, 'm2', $m2);
update_post_meta($post_id, '_construido', $construido);
/*
* Usando tabla adicional
*/
$datos = array(
'm2' => $m2,
'construido' => $construido,
'post_id' => $post_id
);
$where = array(
'post_id' => $post_id
);
$sql = 'SELECT COUNT(*) FROM ' . $wpdb->realty . '
WHERE post_id = ' . intval($post_id);
$count = $wpdb->get_var($sql);
if ( $count > 0 ) {
$wpdb->update($wpdb->realty, $datos, $where);
} else {
$wpdb->insert($wpdb->realty, $datos);
}
}
register_activation_hook(__FILE__, 'jab_br_activate');
function jab_br_activate()
{
global $wpdb;
$sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->realty . " (
id int(11) NOT NULL auto_increment,
post_id int(11) unsigned NOT NULL,
m2 VARCHAR(150) NOT NULL,
construido VARCHAR(150) NOT NULL,
UNIQUE (id)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
$wpdb->query($sql);
// Llama al registro del tipo de dato
jab_br_custom_type();
// purga los rewrites
flush_rewrite_rules();
}
// }}}
// {{{
register_deactivation_hook(__FILE__, 'jab_br_deactivate');
function jab_br_deactivate()
{
// TODO: purgar rewrite rules
}
} else {
// si no estoy en el admin
}
<?php
// TODO: Eliminar tabla
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment