. */ // 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'] = ' '; // 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'] .= ' '.$indent.' '.$cat_name.' '; $alternate++; } } else { $return_html['errors'] .= '

' . __('No categories were found.', $asideshop_domain) . '

'; $return_html['errors'] .= '

' . printf( __('%s operates only with post entries which are placed in categories.', $asideshop_domain), 'AsideShop') . '

'; } ?>

AsideShop

  • /> is_aside() function.)', $asideshop_domain); ?>
  • />
  • />

%foo%), they will not be parsed.', $asideshop_domain); ?>

/> is_aside() function.)', $asideshop_domain); ?>

/>

/>



0) { return TRUE; } else { return FALSE; } } else { return $result; } } /** * asideshop_parse_template() - Parse aside post using template * * @param array $aside_cat_array Array of aside categories intersecting with post categories * @global object $post * @global array $asideshop_patterns * @return string */ function asideshop_parse_template($aside_cat_array = array()) { global $post, $asideshop_patterns; if (empty($aside_cat_array) && !is_array($aside_cat_array)) { return ''; } $as_options = get_option('asideshop_options'); // Get first matching aside category $cat_id = $aside_cat_array[0]; // Get template ID $template_id = $as_options['category_to_template'][$cat_id]; // Get template contents $template = $as_options['templates'][$template_id]['content']; /* There are two replacement methods: a. $play_it_safe = 0 replace only those template tags which are specified in the template. This method requires usage of eval(). Safe as long as $asideshop_patterns are not tampered with. b. $play_it_safe = 1 replace all the template tags at once. Safier, but takes a little bit longer to process huge amounts of posts. */ $play_it_safe = 0; if ($play_it_safe == 0) { // Matched elements are in $matches[0] preg_match_all('/%(.*?)%/i', $template, $matches); // Pattern array with special tags // Loop through matched tags, replace them with content foreach ($matches[0] AS $_match) { if (array_key_exists($_match, $asideshop_patterns)) { eval('$template = str_replace($_match, '.$asideshop_patterns[$_match].', $template);'); } } return $template; } else { $asideshop_patterns_alt = array( 0 => array( "%post_id%", "%post_title%", "%post_content%", "%post_content_filtered%", "%post_excerpt%", "%post_excerpt_filtered%", "%post_permalink%", "%post_date%", "%post_time%", "%post_author%", "%comments_url%", "%comments_count%", "%trackback_url%", ), 1 => array( get_the_ID(), $post->post_title, get_the_content(''), apply_filters('the_content', get_the_content('')), $post->post_excerpt, apply_filters('get_the_excerpt', $post->post_excerpt), get_permalink(), the_date('', '', '', FALSE), get_the_time(), get_the_author(), ($post->comment_count == 0) ? get_permalink() . '#respond' : get_comments_link(), $post->comment_count, trackback_url(FALSE), ), ); $output = str_replace($asideshop_patterns_alt[0], $asideshop_patterns_alt[1], $template); return $output; } } /** * asideshop_display_scripts() - Load javascripts * * @return void */ function asideshop_display_scripts() { // We use JQuery wp_print_scripts(array('jquery')); } /** * asideshop_the_post() - Check whether post needs to be parsed * * @global int $asideshop_ob_started * @return void */ function asideshop_the_post() { if (asideshop_is_enabled() && !is_admin() && !is_feed()) { global $asideshop_ob_started; if ($aside_cat_array = is_aside(TRUE)) { if ($asideshop_ob_started > 0) { ob_end_clean(); $asideshop_ob_started--; } echo asideshop_parse_template($aside_cat_array); ob_start(); $asideshop_ob_started++; } else { if ($asideshop_ob_started > 0) { ob_end_clean(); $asideshop_ob_started--; } } } } /** * asideshop_loop_start() - Initialize upon the beginning of The Loop, extend WP_Query * * @global object $wp_query * @global int $asideshop_ob_started * @return void */ function asideshop_loop_start() { if (asideshop_is_enabled() && !is_admin() && !is_feed() && asideshop_is_frontpage()) { global $wp_query, $asideshop_ob_started; ob_start(); $asideshop_ob_started++; $wp_query = new WP_Query_AsideShop($wp_query); asideshop_the_post(); } } /** * asideshop_loop_end() - Initialize upon the end of The Loop * * @global int $asideshop_ob_started * @global bool $asideshop_loop_ended * @return void */ function asideshop_loop_end() { if (asideshop_is_enabled() && !is_admin() && !is_feed() && asideshop_is_frontpage()) { global $asideshop_ob_started, $asideshop_loop_ended; if ($asideshop_ob_started > 0) { ob_end_clean(); $asideshop_ob_started--; } $asideshop_loop_ended = TRUE; } } /** * WP_Query_AsideShop * * Extend WP_Query with custom the_post() function * * @author Raimonds Kalnins **/ class WP_Query_AsideShop extends WP_Query { /** * WP_Query_AsideShop() * * Initialize WP_Query_AsideShop * * @param object $obj WP_Query object * @return object WP_Query_AsideShop object */ function WP_Query_AsideShop($obj = '') { if ($obj != '' && is_object($obj)) { reset($obj); foreach (get_object_vars($obj) as $key => $value) { $this->$key = $value; } } return $this; } /** * the_post() * * Custom the_post() function * * @return void */ function the_post() { parent::the_post(); asideshop_the_post(); } } // Initiate upon The Loop start add_action('loop_start', 'asideshop_loop_start'); // Initiate upon The Loop end add_action('loop_end', 'asideshop_loop_end'); // We want to use jquery add_action('admin_print_scripts', 'asideshop_display_scripts'); // Display options page add_action('admin_menu', 'asideshop_create_options_page'); // Install script register_activation_hook(__FILE__, 'asideshop_install'); // Uninstall script register_deactivation_hook(__FILE__, 'asideshop_uninstall'); ?>