<?php
/*
Plugin Name: ic BeSocial
Plugin URI: http://wordpress.org/extend/plugins/ic-besocial/
Description: Genera botones para el envío o la votación en distintas redes sociales: Facebook, Twitter, Meneame y Bitacoras.com. Opcionalmente puede mostrar contadores con el número de votos o veces que se ha compartido (según la red).
Author: Jose Cuesta
Version: 1.4
Author URI: http://www.inerciacreativa.com/
*/
/* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ic_Plugin {
var $defaults = array();
var $options = array();
var $id = '';
var $name = '';
function ic_Plugin() {
$this->init();
}
function init() {
$this->id = strtolower(get_class($this));
$this->name = end(explode('_', $this->id));
$this->loadOptions();
}
function addOption( $name, $option ) {
$default = array(
'permanent' => true,
'export' => true,
'type' => 'text',
'value' => '',
'label' => '',
'info' => '',
'enum' => null
);
$option = wp_parse_args($option, $default);
$this->defaults[$name] = $option;
}
/**
* Establece el valor de una opción
*
* @param string $name Nombre de la opción
* @param int|string|array|object $value Valor de la opción
* @return bool Devuelve true si no hay error, false en caso contrario
*/
function setOption( $name, $value ) {
if ( isset($this->defaults[$name]) ) {
settype($value, gettype($this->defaults[$name]['value']));
$this->options[$name] = $value;
return true;
}
return false;
}
/**
* Devuelve el valor de una opción
*
* @param string $name Nombre de la opción
* @return int|string|array|object|null Devuelve el valor o null si no existe
*/
function getOption( $name ) {
$value = null;
if ( isset($this->options[$name]) ) {
$value = $this->options[$name];
} elseif ( isset($this->defaults[$name]['value']) ) {
$value = $this->defaults[$name]['value'];
}
return $value;
}
/**
* Carga las opciones
*/
function loadOptions() {
$this->options = get_option($this->id);
if ( empty($this->options) ) {
$this->options = array();
}
}
/**
* Guarda las opciones de la página de configuración
*/
function saveOptions() {
foreach ( $this->defaults as $name => $option ) {
if ( $option['permanent'] ) {
$value = isset($_POST[$this->name][$name]) ? $_POST[$this->name][$name] : '';
$this->setOption($name, $value);
}
}
return update_option($this->id, $this->options);
}
function getControls() {
echo '<h3 style="border-top:1px dotted #CCC">', ucfirst($this->name), '</h3>';
echo '<table class="form-table">', "\n";
foreach ( $this->defaults as $name => $option ) {
if ( $option['permanent']) {
$this->getControl($name, $this->getOption($name), $option['type'], $option['label'], $option['info'], $option['enum']);
}
}
echo '</table>', "\n";
}
function getControl( $option, $value, $type, $label = '', $info = '', $enum = null ) {
if ( $type == 'text' ) {
$extra = 'class="regular-text"';
$value = esc_attr($value);
} elseif ( $type == 'checkbox' ) {
$extra = ($value) ? 'checked="checked"' : '';
$value = '1';
} elseif ( $type == 'hidden' ) {
$extra = '';
} elseif ( $type == 'select' ) {
$extra = '';
}
if ( isset($extra) ) {
if ( $type != 'hidden' ) {
echo '<tr valign="top">';
if ( $label ) {
printf('<th scope="row"><label for="%s-%s">%s</label></th>', $this->name, trim($option, '_'), $label);
}
echo '<td>';
}
if ( $type != 'select' ) {
printf('<input id="%1$s-%2$s" name="%1$s[%3$s]" type="%4$s" value="%5$s" %6$s/>', $this->name, trim($option, '_'), $option, $type, $value, $extra);
} else {
printf('<select id="%1$s-%2$s" name="%1$s[%3$s]">', $this->name, trim($option, '_'), $option);
foreach ( $enum as $key => $val ) {
$selected = ($key == $value) ? ' selected="selected"' : '';
printf('<option value="%s"%s>%s </option>', $key, $selected, $val);
}
printf('</select>');
}
if ( $type != 'hidden' ) {
if ( $info ) {
printf(' <span class="description">%s</span>', $info);
}
echo '</td></tr>';
}
echo "\n";
}
}
}
class ic_BeSocial extends ic_Plugin {
var $version = '1.4';
var $buttons = array('meneame', 'bitacoras', 'facebook', 'twitter');
var $objects = array();
var $path = '';
var $uri = '';
function init() {
parent::init();
$this->path = plugin_basename(dirname(__FILE__));
$this->uri = plugins_url($this->path);
load_plugin_textdomain('besocial', false, $this->getPath('lang'));
add_action('admin_menu', array(&$this, 'initAdmin'));
add_action('wp_enqueue_scripts', array(&$this, 'initButtons'), 9999);
add_action('the_content', array(&$this, 'showButtons'), 9999);
$this->addOption('position', array('label' => __('Position', 'besocial'), 'type' => 'select', 'value' => 'bottom', 'enum' => array('bottom' => __('Bottom', 'besocial'), 'top' => __('Top', 'besocial'), 'none' => __('None', 'besocial')), 'info' => __('If "None" is selected you must use <code>ic_BeSocial_Buttons()</code> in <code>single.php</code>', 'besocial')));
$this->addOption('alignment', array('label' => __('Alignment', 'besocial'), 'type' => 'select', 'value' => 'center', 'enum' => array('center' => __('Center', 'besocial'), 'left' => __('Left', 'besocial'), 'right' => __('Right', 'besocial'))));
$this->addOption('counters', array('label' => __('Show counters', 'besocial'), 'type' => 'checkbox', 'value' => true));
foreach ( $this->buttons as $name ) {
$this->getButton($name);
}
}
function getPath( $file ) {
return $this->path . '/' . $file;
}
function getURI( $file ) {
return $this->uri . '/' . $file;
}
function &getButton( $name ) {
$name = strtolower($name);
if ( !isset($this->objects[$name]) ) {
$class = __CLASS__ . '_' . ucfirst($name);
$this->objects[$name] =& new $class($this->getOption('counters'));
return $this->objects[$name];
}
return $this->objects[$name];
}
/**
* Añade la página de configuración en el panel de administración
*/
function initAdmin() {
add_options_page('ic BeSocial', 'ic BeSocial', 'manage_options', $this->getPath(basename(__FILE__)), array(&$this, 'getForm'));
}
/**
* Muestra la página de configuración de las opciones
*/
function getForm() {
echo "<div class='wrap'>\n";
printf("<h2>%s ic BeSocial V%s</h2>\n", __('Configure', 'besocial'), $this->version);
if ( isset($_POST['submit']) && $this->setForm() ) {
printf("<div id='message' class='updated fade'><p><strong>%s</strong></p></div>\n", __('Options saved', 'besocial'));
}
printf("<form name='settings' method='post' action='%s?page=%s'>\n", $_SERVER['PHP_SELF'], $this->getPath(basename(__FILE__)));
wp_nonce_field($this->id);
$this->getControls();
foreach ( $this->buttons as $name ) {
$button =& $this->getButton($name);
$button->getControls();
}
printf("<p class='submit'><input class='button-primary' type='submit' value='%s' name='submit' /></p>\n", __('Update', 'besocial'));
echo "</form>\n</div>\n";
}
/**
* Guarda las opciones
*/
function setForm() {
check_admin_referer($this->id);
$update = $this->saveOptions();
foreach ( $this->buttons as $name ) {
$button =& $this->getButton($name);
$update = $button->saveOptions() || $update;
}
return $update;
}
/**
* Inicializa el plugin
*/
function initButtons() {
$post = $this->getPost();
if ( !empty($post) ) {
$vars = array();
foreach ( $this->buttons as $name ) {
$button =& $this->getButton($name);
$vars = array_merge($vars, $button->setPost($post));
}
$this->getStyles();
$this->getScripts($vars);
}
}
/**
* Obtiene los datos necesarios del post
*/
function getPost() {
global $post;
$data = array();
if ( is_single() ) {
the_post();
if ( $post->post_status == 'publish' && $post->post_password == '' ) {
$data = array(
'ID' => $post->ID,
'title' => get_the_title(),
'url' => get_permalink()
);
}
rewind_posts();
}
return $data;
}
/**
* Inserta las hojas de estilos
*
* @param string $source URL relativa al plugin
*/
function getStyles() {
global $wptouch_plugin;
$file = 'besocial.css';
if ( is_a($wptouch_plugin, 'WPtouchPlugin') && $wptouch_plugin->desired_view == 'mobile' ) {
$file = 'besocial-mobile.css';
}
wp_enqueue_style('besocial', $this->getURI($file), false, $this->version, 'all');
}