. */ // Prevent direct access to the plugin if (!defined('ABSPATH')) { exit(); } // Version number, may be used to update things in the future global $asideshop_version; $asideshop_version = '1.0.8'; // Domain for I18N global $asideshop_domain; $asideshop_domain = 'asideshop'; // Variable lets us know whether the loop has been ended. global $asideshop_loop_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. global $asideshop_ob_started; $asideshop_ob_started = 0; // Template tags, used in templates global $asideshop_patterns; $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_date_regular%" => 'get_the_time(get_option(\'date_format\'))', "%post_time%" => 'get_the_time()', "%post_author%" => 'get_the_author()', "%post_categories%" => 'get_the_category_list(\', \')', "%post_tags%" => '((float)$wp_version >= (float)"2.3" ? get_the_term_list(0, \'post_tag\', \'\', \', \') : \'\') ', "%comments_url%" => '($post->comment_count == 0) ? get_permalink() . \'#respond\' : get_comments_link()', "%comments_count%" => '$post->comment_count', "%trackback_url%" => 'trackback_url(FALSE)', ); // Options where to display aside posts global $asideshop_show_on_array; $asideshop_show_on_array = array('front_page' => 1, 'category_view' => 0, 'tag_view' => 0, 'archive_view' => 0, 'date_view' => 0, 'author_view' => 0, 'search_view' => 1); /** // 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( array( 'title' => '', // string 'content' => '', // string ), ), 'show_asides_on' => array( 'front_page' => 1, // integer 'category_view' => 0, // integer 'tag_view' => 0, // integer 'archive_view' => 0, // integer 'date_view' => 0, // integer 'author_view' => 0, // integer 'search_view' => 1, // integer ), 'category_to_template' => array( '' => '' // array(category_id => template_id) ), ); */ /** * asideshop_install() - Install plugin * * @global string $asideshop_version * @global string $asideshop_show_on_array * @return void */ function asideshop_install() { global $asideshop_version, $asideshop_show_on_array; add_option('asideshop_options', array('enabled' => 0, 'version' => $asideshop_version, 'show_asides' => array(), 'templates' => array(), 'category_to_template' => array(), 'show_asides_on' => $asideshop_show_on_array)); } /** * 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() { $asideshop_options = get_option('asideshop_options'); if ((asideshop_has_loop_ended() === FALSE && $asideshop_options['enabled'] == 1) || ($asideshop_options['enabled'] == 2 && current_user_can('activate_plugins'))) { return TRUE; } return FALSE; } /** * asideshop_is_displayable() - Check whether asides should be displayed * * @global array $asideshop_version * @return bool */ function asideshop_is_displayable() { global $asideshop_version; $asideshop_options = get_option('asideshop_options'); if (!array_key_exists('show_asides_on', $asideshop_options)) { return asideshop_is_frontpage(); } else { if ($asideshop_options['show_asides_on']['front_page'] == 1 && asideshop_is_frontpage()) { return TRUE; } else if ($asideshop_options['show_asides_on']['search_view'] == 1 && is_search()) { return TRUE; } else if ($asideshop_options['show_asides_on']['archive_view'] == 1 && is_archive()) { return TRUE; } else if ($asideshop_options['show_asides_on']['category_view'] == 1 && is_category()) { return TRUE; } else if (function_exists('is_tag') && $asideshop_options['show_asides_on']['tag_view'] == 1 && is_tag()) { return TRUE; } else if ($asideshop_options['show_asides_on']['date_view'] == 1 && is_date()) { return TRUE; } else if ($asideshop_options['show_asides_on']['author_view'] == 1 && is_author()) { 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') && is_tag()) { $is_tag = TRUE; } if (!is_archive() && !is_single() && !$is_tag && !is_search()) { 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; asideshop_upgrade(); load_plugin_textdomain($asideshop_domain, 'wp-content/plugins/' . $asideshop_domain); } /** * asideshop_upgrade() - Upgrade AsideShop options * * @return void */ function asideshop_upgrade() { global $asideshop_show_on_array; $asideshop_options = get_option('asideshop_options'); // Upgrade if (is_array($asideshop_options)) { if (!array_key_exists('show_asides_on', $asideshop_options)) { update_option('asideshop_options', $asideshop_options + array('show_asides_on' => $asideshop_show_on_array)); } else { $asideshop_options['show_asides_on'] = $asideshop_options['show_asides_on'] + $asideshop_show_on_array; update_option('asideshop_options', $asideshop_options); } } else { asideshop_install(); } } /** * asideshop_nonce_field() - Show nonce field if available * * @return mixed */ function asideshop_nonce_field() { if (!function_exists('wp_nonce_field')) { return; } else { return wp_nonce_field('asideshop-options'); } } /** * asideshop_conf() - Display options page * * @global array $asideshop_patterns * @global string $asideshop_domain * @global string $wp_version * @global string $asideshop_show_on_array * @return string */ function asideshop_conf() { global $asideshop_patterns, $asideshop_domain, $wp_version, $asideshop_show_on_array; asideshop_setup(); if (!empty($_POST)) { check_admin_referer('asideshop-options'); $asideshop_options = get_option('asideshop_options'); $new_options = $_POST['asideshop_options']; if (!empty($new_options)) { $options_to_update = array('templates' => array(), 'version' => $asideshop_options['version']); 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']; } if (!empty($new_options['show_asides_on'])) { $options_to_update['show_asides_on'] = $new_options['show_asides_on']; } // 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 'show_asides_on' option foreach ($asideshop_show_on_array AS $new_show_on_key => $show_on_value) { if (isset($new_options['show_asides_on'][$new_show_on_key])) { $new_show_on_value = 1; } else { $new_show_on_value = 0; } $options_to_update['show_asides_on'][$new_show_on_key] = $new_show_on_value; } // 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) || ($_match == '%post_tags%' && (float)$wp_version < (float)"2.3") ) { $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']; // Matched elements are in $matches[0] preg_match_all('/%(.*?)%/i', $template, $matches); // 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; } /** * 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()&&!is_single()) { 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_displayable()) { global $asideshop_ob_started; ob_start(); $asideshop_ob_started++; } } /** * 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_displayable()) { global $asideshop_ob_started, $asideshop_loop_ended; if ($asideshop_ob_started > 0) { ob_end_clean(); $asideshop_ob_started--; } $asideshop_loop_ended = TRUE; } } // Hook up the_post processing add_action('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'); ?>