.
*/
// Prevent direct access to the plugin
if (!defined('ABSPATH')) {
exit();
}
// Version number, may be used to update things in the future
$asideshop_version = '1.0.3';
// Domain for I18N
$asideshop_domain = 'asideshop';
// Variable lets us know whether the loop has been ended.
$asideshop_loop_ended = FALSE;
// Variable which permits one ob_end_clean() for every ob_start(). Without it other plugins which use ob_start() break AsideShop.
$asideshop_ob_started = 0;
// Template tags, used in templates
$asideshop_patterns = array(
"%post_id%" => 'get_the_ID()',
"%post_title%" => '$post->post_title',
"%post_content%" => 'get_the_content(\'\')',
"%post_content_filtered%" => 'apply_filters(\'the_content\', get_the_content(\'\'))',
"%post_excerpt%" => '$post->post_excerpt',
"%post_excerpt_filtered%" => 'apply_filters(\'get_the_excerpt\', $post->post_excerpt)',
"%post_permalink%" => 'get_permalink()',
"%post_date%" => 'the_date(\'\', \'\', \'\', FALSE)',
"%post_time%" => 'get_the_time()',
"%post_author%" => 'get_the_author()',
"%comments_url%" => '($post->comment_count == 0) ? get_permalink() . \'#respond\' : get_comments_link()',
"%comments_count%" => '$post->comment_count',
"%trackback_url%" => 'trackback_url(FALSE)',
);
/**
// This plugin uses these options created upon plugin installation.
$as_options = array(
'enabled' => '', // 0 - disabled || 1 - enabled || 2 - testing
'version' => '', // string (e.g. 1.0)
'show_asides' => array(), // array(category_id => 1)
'templates' => array(
'title' => '', // string
'content' => '', // string
),
'category_to_template' => array(
'' => '' // array(category_id => template_id)
),
);
*/
/**
* asideshop_install() - Install plugin
*
* @global string $asideshop_version
* @return void
*/
function asideshop_install()
{
global $asideshop_version;
$asideshop_options = get_option('asideshop_options');
if (empty($asideshop_options)) {
add_option('asideshop_options', array('enabled' => 0,
'version' => $asideshop_version,
'show_asides' => array(),
'templates' => array(),
'category_to_template' => array()));
} else {
update_option('asideshop_options', $asideshop_options + array('version' => $asideshop_version));
}
}
/**
* asideshop_uninstall() - Uninstall plugin and remove all the options
*
* @return void
*/
function asideshop_uninstall()
{
delete_option('asideshop_options');
}
/**
* asideshop_create_options_page() - Initialize options page
*
* @return void
*/
function asideshop_create_options_page()
{
add_options_page('AsideShop', 'AsideShop', 9, basename(__FILE__), 'asideshop_conf');
}
/**
* $asideshop_has_loop_ended() - Check whether we are in The Loop
*
* @global bool $asideshop_loop_ended
*/
function asideshop_has_loop_ended()
{
global $asideshop_loop_ended;
if ($asideshop_loop_ended === TRUE) {
return TRUE;
}
return FALSE;
}
/**
* asideshop_is_enabled() - Check whether plugin is enabled
*
* @return bool
*/
function asideshop_is_enabled()
{
$as_options = get_option('asideshop_options');
if (asideshop_has_loop_ended() === FALSE && $as_options['enabled'] == 1 || ($as_options['enabled'] == 2 && current_user_can('activate_plugins'))) {
return TRUE;
}
return FALSE;
}
/**
* asideshop_is_frontpage() - Check whether current page is a frontpage
*
* @return bool
*/
function asideshop_is_frontpage()
{
// Backward compatibility with 2.2, pre-taxonomy version.
$is_tag = FALSE;
if (function_exists('is_tag')) {
if (is_tag()) {
$is_tag = TRUE;
}
}
if (!is_category() && !is_single() && !$is_tag && !is_archive()) {
return TRUE;
}
return FALSE;
}
/**
* asideshop_get_categories() - Get categories
*
* This function is used to get post categories rather than all categories.
*
* @global string $wp_version
* @global object $wpdb
* @return array
*/
function asideshop_get_categories()
{
global $wp_version, $wpdb;
// If 2.2.x
if ((float)$wp_version < (float)"2.3") {
$categories = $wpdb->get_results("SELECT `cat_ID`, `cat_name`, `category_parent` FROM `{$wpdb->categories}` WHERE `link_count` = 0 ORDER BY `cat_name` ASC");
} else {
// If 2.3 and later
$categories = get_categories('hide_empty=0');
}
$result = array();
if (!empty($categories) && is_array($categories)) {
foreach ($categories AS $_cat) {
// Backward compatibility with 2.2, pre-taxonomy version.
if (empty($_cat->term_id)) {
$cat_id = $_cat->cat_ID;
$cat_name = $_cat->cat_name;
$cat_parent = $_cat->category_parent;
} else {
$cat_id = $_cat->term_id;
$cat_name = $_cat->name;
$cat_parent = $_cat->category_parent;
}
$result[] = array(
'cat_id' => $cat_id,
'cat_name' => $cat_name,
'cat_parent' => $cat_parent,
);
}
}
return $result;
}
/**
* asideshop_setup() - Run misc. things before configuration panel appears
*
* @global string $asideshop_domain
* @return string
*/
function asideshop_setup()
{
global $asideshop_domain;
load_plugin_textdomain($asideshop_domain, 'wp-content/plugins/' . $asideshop_domain);
}
/**
* asideshop_conf() - Display options page
*
* @global array $asideshop_patterns
* @global string $asideshop_domain
* @global string $wp_version
* @return string
*/
function asideshop_conf()
{
global $asideshop_patterns, $asideshop_domain, $wp_version;
asideshop_setup();
if (!empty($_POST)) {
check_admin_referer('asideshop_options');
$asideshop_options = get_option('asideshop_options');
$new_options = $_POST['asideshop_options'];
$options_to_update = array('templates' => array());
if (!empty($new_options)) {
if (isset($new_options['enabled'])) {
$options_to_update['enabled'] = $new_options['enabled'];
}
if (!empty($new_options['show_asides'])) {
$options_to_update['show_asides'] = $new_options['show_asides'];
}
// Delete templates
$c2t_to_remove = array();
if (!empty($asideshop_options['templates'])) {
foreach ($asideshop_options['templates'] AS $current_template_key => $current_template) {
if (!empty($new_options['delete_templates'][$current_template_key])) {
unset(
// Delete templates which were not edited
$asideshop_options['templates'][$current_template_key],
// Delete templates which were edited
$new_options['templates'][$current_template_key]
);
$c2t_to_remove[] = $current_template_key;
}
}
$options_to_update['templates'] = $asideshop_options['templates'];
}
// Category to template, if template is deleted, remove from category/template relation
if (!empty($new_options['category_to_template'])) {
foreach ($new_options['category_to_template'] AS $c2t_key => $c2t) {
// If default template, display post as regular
if (empty($c2t)) {
unset($options_to_update['show_asides'][$c2t_key]);
}
// If template is deleted, remove from category/template relation
if (in_array($c2t, $c2t_to_remove)) {
$options_to_update['category_to_template'][$c2t_key] = '';
// Display also post as regular
unset($options_to_update['show_asides'][$c2t_key]);
} else {
$options_to_update['category_to_template'][$c2t_key] = $c2t;
}
}
}
// New templates would be added, edited templates would be overwritten
if (!empty($new_options['templates'])) {
foreach ($new_options['templates'] AS $new_template_key => $new_template) {
if ($new_template['title'] == '') {
// Do not add empty templates
if ($new_template['content'] == '') {
continue;
}
// Add default title if title is empty
$new_template['title'] = 'Template-' . $new_template_key;
}
// Strip slashes from title and content
array_walk($new_template, create_function('&$a', '$a = stripslashes($a);'));
$options_to_update['templates'][$new_template_key] = $new_template;
}
}
// Update all options
update_option('asideshop_options', $options_to_update);
}
?>
'', 'categories' => '');
// Available tags
$return_html['available_tags'] = '
' . __('Available tags:', $asideshop_domain) . '
' . __('Post tags:', $asideshop_domain) . '
%post_id%
%post_title%
%post_content%
%post_content_filtered%
%post_excerpt%
%post_excerpt_filtered%
%post_permalink%
%post_date%
%post_time%
%post_author%
' . __('Comment tags:', $asideshop_domain) . '
%comments_url%
%comments_count%
';
// Initial last template array element ID, needed to determine new template element ID
$max_template_id = 0;
// Template Loop
if (!empty($asideshop_options['templates']) && is_array($asideshop_options['templates'])) {
// Show odd rows in different color
$alternate = 0;
// Get last template array element ID, needed to determine new template element ID
$max_template_id = max(array_keys($asideshop_options['templates']));
foreach ($asideshop_options['templates'] AS $_tmpl_key => $_tmpl) {
// Show alternates
$tr_alternate = '';
if ($alternate % 2 == 0) {
$tr_alternate = ' class="alternate"';
}
// Parse content to be display correctly
$content = str_replace(array("\r\n", "\n", "\r", "\t"), array('\n', '\n', '\n', '\t'), $_tmpl['content']);
// Content for javascript
$js_content = preg_replace('/<\/(\w+)>/i', '<\/${1}>', $content);
// Matched template elements are in $matches[0]
preg_match_all('/%(.*?)%/i', $_tmpl['content'], $matches);
// Content for listing
$content = htmlentities($_tmpl['content']);
foreach ($matches[0] AS $_match) {
$e = ''; // error state css
if (!array_key_exists($_match, $asideshop_patterns)) {
$e = ' style="color: #f00000;"';
}
$content = str_replace($_match, "{$_match}", $content);
}
$return_html['templates'] .= '
'.$_tmpl['title'].'
'.wordwrap($content).'
';
$alternate++;
}
}
// Get all categories using WordPress built-in function
// type=post is for WordPress 2.2 version
$categories = asideshop_get_categories();
// Category Loop
if (!empty($categories)) {
// Show odd rows in different color
$alternate = 0;
// Indent subcategories with dashes
$indent = '';
foreach ($categories AS $_cat) {
// Get $cat_id, $cat_name, $cat_parent
extract($_cat);
// Show subcategories
if ($cat_parent == 0) {
$indent = '';
} else {
$indent .= '—';
}
// Show alternates
$tr_alternate = '';
if ($alternate % 2 == 0) {
$tr_alternate = ' class="alternate"';
}
// Mark asides checkbox as selected
$asides_selected = '';
if (isset($asideshop_options['show_asides'][$cat_id]) && $asideshop_options['show_asides'][$cat_id] > 0) {
$asides_selected = "checked='checked'";
}
// Category to template options
$category_to_template = '';
if (!empty($asideshop_options['templates']) && is_array($asideshop_options['templates'])) {
foreach ($asideshop_options['templates'] AS $_tmpl_key => $_tmpl) {
$sel = '';
if ($asideshop_options['category_to_template'][$cat_id] == $_tmpl_key) {
$sel = ' selected="selected"';
}
$category_to_template .= '';
}
}
$return_html['categories'] .= '