Reformating

This commit is contained in:
Eric van der Vlist 2020-05-09 12:15:44 +02:00
parent eccd03b749
commit 9c5f86fc7b
1 changed files with 510 additions and 512 deletions

View File

@ -1,12 +1,12 @@
<?php <?php
/* /*
Plugin Name: Extended Table of Contents (with nextpage support) Plugin Name: Extended Table of Contents (with nextpage support)
Plugin URI: http://http://www.web-cloud-apps.com/produkte/wordpress-extended-toc/ Plugin URI: http://http://www.web-cloud-apps.com/produkte/wordpress-extended-toc/
Description: This plugin automatically generates and inserts a table of contents (ToC) to your pages and posts, based on tags h1-h6. Whenever the plugin discovers more than a certain amount of headings (default: 3) the ToC is inserted at the top of the page. This plugin also can handle posts that are divided into pages by the nextpage-wordpress-tag. Any feedback or suggestions are welcome. Description: This plugin automatically generates and inserts a table of contents (ToC) to your pages and posts, based on tags h1-h6. Whenever the plugin discovers more than a certain amount of headings (default: 3) the ToC is inserted at the top of the page. This plugin also can handle posts that are divided into pages by the nextpage-wordpress-tag. Any feedback or suggestions are welcome.
Version: 1.0.0.1 Version: 1.0.0.1
Author: Daniel Boldura, Web.Cloud.Apps. GmbH Author: Daniel Boldura, Web.Cloud.Apps. GmbH
Author URI: http://www.web-cloud-apps.com Author URI: http://www.web-cloud-apps.com
Gitea Plugin URI: https://gitea.dyomedea.com/Dyomedea/extended-table-of-contents-with-nextpage-support Gitea Plugin URI: https://gitea.dyomedea.com/Dyomedea/extended-table-of-contents-with-nextpage-support
*/ */
@ -25,17 +25,18 @@ Gitea Plugin URI: https://gitea.dyomedea.com/Dyomedea/extended-table-of-contents
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
define( 'EXTENDED_TOC_VERSION', '1.0.0' ); define('EXTENDED_TOC_VERSION', '1.0.0');
define( 'EXTENDED_TOC_ID', 'extended_toc' ); define('EXTENDED_TOC_ID', 'extended_toc');
define( 'EXTENDED_TOC_NAME', 'Extended-ToC' ); define('EXTENDED_TOC_NAME', 'Extended-ToC');
define( 'TOC_MIN_START', 2 ); define('TOC_MIN_START', 2);
define( 'TOC_MAX_START', 10 ); define('TOC_MAX_START', 10);
if (!class_exists('ExToC')) {
class ExToC {
if( !class_exists('ExToC') ) {
class ExToC
{
private $path; private $path;
private $content = ""; private $content = "";
private $fullcontent = ""; private $fullcontent = "";
@ -46,132 +47,129 @@ if( !class_exists('ExToC') ) {
private $minLevel = null; private $minLevel = null;
public function __construct() { public function __construct() {
$this->path = plugins_url( '', __FILE__ ); $this->path = plugins_url('', __FILE__);
$this->exclude_post_types = array( 'attachment', 'revision', 'nav_menu_item', 'safecss' ); $this->exclude_post_types = array('attachment', 'revision', 'nav_menu_item', 'safecss');
// get options // get options
$defaults = array( // default options $defaults = array(// default options
'heading_text' => 'Contents', 'heading_text' => 'Contents',
'start' => 3, 'start' => 3,
'show_heading_text' => true, 'show_heading_text' => true,
'auto_insert_post_types' => array('page', 'post'), 'auto_insert_post_types' => array('page', 'post'),
'heading_levels' => array('1', '2' ,'3', '4', '5', '6'), 'heading_levels' => array('1', '2', '3', '4', '5', '6'),
'show_hierarchy' => true, 'show_hierarchy' => true,
'number_list_items' => true, 'number_list_items' => true,
); );
$options = get_option( EXTENDED_TOC_ID, $defaults ); $options = get_option(EXTENDED_TOC_ID, $defaults);
$this->options = wp_parse_args( $options, $defaults ); $this->options = wp_parse_args($options, $defaults);
add_action( 'plugins_loaded', array(&$this, 'plugins_loaded') ); add_action('plugins_loaded', array(&$this, 'plugins_loaded'));
if( is_admin() ) { if (is_admin()) {
//Additional links on the plugin page //Additional links on the plugin page
add_filter('plugin_row_meta', array(&$this, 'register_plugin_links'), 10, 2); add_filter('plugin_row_meta', array(&$this, 'register_plugin_links'), 10, 2);
add_action('admin_init', array(&$this, 'admin_init')); add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'admin_menu')); add_action('admin_menu', array(&$this, 'admin_menu'));
} } else {
else { /** Add the content filter and enqueue css * */
/** Add the content filter and enqueue css **/ add_filter('the_content', array(&$this, 'the_content'), 100);
add_filter( 'the_content', array(&$this, 'the_content'), 100 ); add_action('wp_enqueue_scripts', array(&$this, 'wp_enqueue_scripts'));
add_action( 'wp_enqueue_scripts', array(&$this, 'wp_enqueue_scripts') );
add_shortcode( 'extoc', array(&$this, 'shortcode_extoc') ); add_shortcode('extoc', array(&$this, 'shortcode_extoc'));
add_shortcode( 'noextoc', array(&$this, 'shortcode_noextoc') ); add_shortcode('noextoc', array(&$this, 'shortcode_noextoc'));
} }
} }
public function __destruct() { public function __destruct() {
} }
public function register_plugin_links($links, $file) { public function register_plugin_links($links, $file) {
if( $file == plugin_basename(__FILE__) ) { if ($file == plugin_basename(__FILE__)) {
$links[] = '<a href="http://http://www.web-cloud-apps.com/produkte/wordpress-extended-toc">' . __('Donate', EXTENDED_TOC_ID) . '</a>'; $links[] = '<a href="http://http://www.web-cloud-apps.com/produkte/wordpress-extended-toc">' . __('Donate', EXTENDED_TOC_ID) . '</a>';
} }
return $links; return $links;
} }
public function admin_init() { public function admin_init() {
wp_register_style( EXTENDED_TOC_ID, $this->path . '/admin-style.css', array(), EXTENDED_TOC_VERSION ); wp_register_style(EXTENDED_TOC_ID, $this->path . '/admin-style.css', array(), EXTENDED_TOC_VERSION);
wp_enqueue_style(EXTENDED_TOC_ID); wp_enqueue_style(EXTENDED_TOC_ID);
} }
public function admin_menu() { public function admin_menu() {
// Create menu tab // Create menu tab
$page = add_submenu_page( 'plugins.php', EXTENDED_TOC_NAME, EXTENDED_TOC_NAME, 'manage_options', EXTENDED_TOC_ID, array(&$this, 'admin_options') ); $page = add_submenu_page('plugins.php', EXTENDED_TOC_NAME, EXTENDED_TOC_NAME, 'manage_options', EXTENDED_TOC_ID, array(&$this, 'admin_options'));
} }
private function save_admin_options() private function save_admin_options() {
{
global $post_id; global $post_id;
// security check // security check
if ( !wp_verify_nonce( @$_POST[EXTENDED_TOC_ID], plugin_basename(__FILE__) ) ) if (!wp_verify_nonce(@$_POST[EXTENDED_TOC_ID], plugin_basename(__FILE__)))
return false; return false;
// require an administrator level to save // require an administrator level to save
if ( !current_user_can( 'manage_options', $post_id ) ) if (!current_user_can('manage_options', $post_id))
return false; return false;
$this->options = array_merge( $this->options = array_merge(
$this->options, $this->options,
array( array(
'heading_text' => stripslashes( trim($_POST['heading_text']) ), 'heading_text' => stripslashes(trim($_POST['heading_text'])),
'auto_insert_post_types' => @(array)$_POST['auto_insert_post_types'], 'auto_insert_post_types' => @(array) $_POST['auto_insert_post_types'],
'start' => intval($_POST['start']), 'start' => intval($_POST['start']),
'show_heading_text' => (isset($_POST['show_heading_text']) && $_POST['show_heading_text']) ? true : false, 'show_heading_text' => (isset($_POST['show_heading_text']) && $_POST['show_heading_text']) ? true : false,
'show_hierarchy' => (isset($_POST['show_hierarchy']) && $_POST['show_hierarchy']) ? true : false, 'show_hierarchy' => (isset($_POST['show_hierarchy']) && $_POST['show_hierarchy']) ? true : false,
'number_list_items' => (isset($_POST['number_list_items']) && $_POST['number_list_items']) ? true : false, 'number_list_items' => (isset($_POST['number_list_items']) && $_POST['number_list_items']) ? true : false,
'heading_levels' => @(array)$_POST['heading_levels'], 'heading_levels' => @(array) $_POST['heading_levels'],
) )
); );
// update_option will return false if no changes were made // update_option will return false if no changes were made
update_option( EXTENDED_TOC_ID, $this->options ); update_option(EXTENDED_TOC_ID, $this->options);
return true; return true;
} }
public function admin_options() { public function admin_options() {
if( isset($_GET['update']) ) { if (isset($_GET['update'])) {
if( $this->save_admin_options() ) if ($this->save_admin_options())
$msg = '<div id="message" class="updated fade"><p>' . __('Options saved.', EXTENDED_TOC_ID) . '</p></div>'; $msg = '<div id="message" class="updated fade"><p>' . __('Options saved.', EXTENDED_TOC_ID) . '</p></div>';
else else
$msg = '<div id="message" class="error fade"><p>' . __('Save failed.', EXTENDED_TOC_ID) . '</p></div>'; $msg = '<div id="message" class="error fade"><p>' . __('Save failed.', EXTENDED_TOC_ID) . '</p></div>';
} }
?> ?>
<div class="wrap"> <div class="wrap">
<div id="icon-plugins" class="icon32"> <div id="icon-plugins" class="icon32">
<br /> <br />
</div> </div>
<h2><?php echo __("Extended Table of Contents", EXTENDED_TOC_ID)?></h2> <h2><?php echo __("Extended Table of Contents", EXTENDED_TOC_ID) ?></h2>
<?php echo $msg; ?> <?php echo $msg; ?>
<h2 class="nav-tab-wrapper"> <h2 class="nav-tab-wrapper">
<a class="nav-tab<?php echo !$_GET['tab']||$_GET['tab']==''?' nav-tab-active':''; ?>" href="?page=<?php echo $_GET['page']; ?>"><?php _e('Main Options', 'EXTENDED_TOC_ID'); ?></a> <a class="nav-tab<?php echo!$_GET['tab'] || $_GET['tab'] == '' ? ' nav-tab-active' : ''; ?>" href="?page=<?php echo $_GET['page']; ?>"><?php _e('Main Options', 'EXTENDED_TOC_ID'); ?></a>
<a class="nav-tab<?php echo $_GET['tab']=='help'?' nav-tab-active':''; ?>" href="?page=<?php echo $_GET['page']; ?>&amp;tab=help"><?php _e('Help', 'EXTENDED_TOC_ID'); ?></a> <a class="nav-tab<?php echo $_GET['tab'] == 'help' ? ' nav-tab-active' : ''; ?>" href="?page=<?php echo $_GET['page']; ?>&amp;tab=help"><?php _e('Help', 'EXTENDED_TOC_ID'); ?></a>
</h2> </h2>
<?php <?php
if( !$_GET['tab'] || $_GET['tab'] == '' ) { if (!$_GET['tab'] || $_GET['tab'] == '') {
$this->displayMainContent(); $this->displayMainContent();
} } else {
else {
$this->displayHelpContent(); $this->displayHelpContent();
} }
?> ?>
</div> </div>
<?php <?php
} }
private function displayMainContent() { private function displayMainContent() {
?> ?>
<form method="post" action="<?php echo htmlentities('?page=' . $_GET['page'] . '&update'); ?>"> <form method="post" action="<?php echo htmlentities('?page=' . $_GET['page'] . '&update'); ?>">
<?php wp_nonce_field( plugin_basename(__FILE__), EXTENDED_TOC_ID ); ?> <?php wp_nonce_field(plugin_basename(__FILE__), EXTENDED_TOC_ID); ?>
<div class="form_container"> <div class="form_container">
<table class="form-table"> <table class="form-table">
@ -179,7 +177,7 @@ if( !class_exists('ExToC') ) {
<tr> <tr>
<th><label for="show_heading_text"><?php echo __('Show heading text', EXTENDED_TOC_ID); ?></label></th> <th><label for="show_heading_text"><?php echo __('Show heading text', EXTENDED_TOC_ID); ?></label></th>
<td> <td>
<input id="show_heading_text" type="checkbox" name="show_heading_text" <?php if ( $this->options['show_heading_text'] ) echo ' checked="checked"'; ?> /> <input id="show_heading_text" type="checkbox" name="show_heading_text" <?php if ($this->options['show_heading_text']) echo ' checked="checked"'; ?> />
</td> </td>
</tr> </tr>
@ -191,10 +189,10 @@ if( !class_exists('ExToC') ) {
<tr> <tr>
<th><?php echo __('Add table of contents to following content types', EXTENDED_TOC_ID); ?></th> <th><?php echo __('Add table of contents to following content types', EXTENDED_TOC_ID); ?></th>
<td> <td>
<?php foreach( get_post_types() as $post_type ): ?> <?php foreach (get_post_types() as $post_type): ?>
<?php if( !in_array($post_type, $this->exclude_post_types) ): ?> <?php if (!in_array($post_type, $this->exclude_post_types)): ?>
<input type="checkbox" value="<?php echo $post_type?>" id="auto_insert_post_types_<?php echo $post_type?>" name="auto_insert_post_types[]"<?php echo in_array($post_type, $this->options['auto_insert_post_types'])?' checked="checked"':''?> /> <input type="checkbox" value="<?php echo $post_type ?>" id="auto_insert_post_types_<?php echo $post_type ?>" name="auto_insert_post_types[]"<?php echo in_array($post_type, $this->options['auto_insert_post_types']) ? ' checked="checked"' : '' ?> />
<label for="auto_insert_post_types_<?php echo $post_type?>"><?php echo $post_type?></label><br /> <label for="auto_insert_post_types_<?php echo $post_type ?>"><?php echo $post_type ?></label><br />
<?php endif; ?> <?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
</td> </td>
@ -207,7 +205,8 @@ if( !class_exists('ExToC') ) {
<?php <?php
for ($i = TOC_MIN_START; $i <= TOC_MAX_START; $i++) { for ($i = TOC_MIN_START; $i <= TOC_MAX_START; $i++) {
echo '<option value="' . $i . '"'; echo '<option value="' . $i . '"';
if ( $i == $this->options['start'] ) echo ' selected="selected"'; if ($i == $this->options['start'])
echo ' selected="selected"';
echo '>' . $i . '</option>' . "\n"; echo '>' . $i . '</option>' . "\n";
} }
?> ?>
@ -219,14 +218,14 @@ if( !class_exists('ExToC') ) {
<tr> <tr>
<th><label for="show_hierarchy"><?php echo __('Show hierarchy', EXTENDED_TOC_ID); ?></label></th> <th><label for="show_hierarchy"><?php echo __('Show hierarchy', EXTENDED_TOC_ID); ?></label></th>
<td> <td>
<input id="show_hierarchy" type="checkbox" name="show_hierarchy" <?php if ( $this->options['show_hierarchy'] ) echo ' checked="checked"'; ?> /> <input id="show_hierarchy" type="checkbox" name="show_hierarchy" <?php if ($this->options['show_hierarchy']) echo ' checked="checked"'; ?> />
</td> </td>
</tr> </tr>
<tr> <tr>
<th><label for="number_list_items"><?php echo __('Number list items', EXTENDED_TOC_ID); ?></label></th> <th><label for="number_list_items"><?php echo __('Number list items', EXTENDED_TOC_ID); ?></label></th>
<td> <td>
<input id="number_list_items" type="checkbox" name="number_list_items" <?php if ( $this->options['number_list_items'] ) echo ' checked="checked"'; ?> /> <input id="number_list_items" type="checkbox" name="number_list_items" <?php if ($this->options['number_list_items']) echo ' checked="checked"'; ?> />
</td> </td>
</tr> </tr>
@ -238,9 +237,10 @@ if( !class_exists('ExToC') ) {
<?php <?php
// show heading 1 to 6 options // show heading 1 to 6 options
for ($i = 1; $i <= 6; $i++) { for ($i = 1; $i <= 6; $i++) {
echo '<input type="checkbox" value="' . $i . '" id="heading_levels' . $i .'" name="heading_levels[]"'; echo '<input type="checkbox" value="' . $i . '" id="heading_levels' . $i . '" name="heading_levels[]"';
if ( in_array($i, $this->options['heading_levels']) ) echo ' checked="checked"'; if (in_array($i, $this->options['heading_levels']))
echo ' /><label for="heading_levels' . $i .'"> ' . __('heading ') . $i . ' - h' . $i . '</label><br />'; echo ' checked="checked"';
echo ' /><label for="heading_levels' . $i . '"> ' . __('heading ') . $i . ' - h' . $i . '</label><br />';
} }
?> ?>
</td> </td>
@ -249,14 +249,14 @@ if( !class_exists('ExToC') ) {
</table> </table>
</div> </div>
<p class="submit"><input class="button-primary" type="submit" value="<?php echo __("Save Options", EXTENDED_TOC_ID)?>" name="submit" /></p> <p class="submit"><input class="button-primary" type="submit" value="<?php echo __("Save Options", EXTENDED_TOC_ID) ?>" name="submit" /></p>
</form> </form>
<?php <?php
} }
// ToDo: Also allow to markup settings Hierarchie and Kapitelnummer // ToDo: Also allow to markup settings Hierarchie and Kapitelnummer
private function displayHelpContent() { private function displayHelpContent() {
?> ?>
<div class="form_container"> <div class="form_container">
<h3>Position the ToC</h3> <h3>Position the ToC</h3>
<div> <div>
@ -289,7 +289,7 @@ if( !class_exists('ExToC') ) {
You can also remove the title by adding "notitle" e.g. [extoc notitle]. Leaving the title attribute empty will also take the header defined within the general plugin settings. You can also remove the title by adding "notitle" e.g. [extoc notitle]. Leaving the title attribute empty will also take the header defined within the general plugin settings.
</div> </div>
</div> </div>
<?php <?php
} }
public function wp_enqueue_scripts() { public function wp_enqueue_scripts() {
@ -298,27 +298,30 @@ if( !class_exists('ExToC') ) {
} }
public function plugins_loaded() { public function plugins_loaded() {
load_plugin_textdomain( EXTENDED_TOC_ID, false, dirname(plugin_basename(__FILE__)) . '/locale/' ); load_plugin_textdomain(EXTENDED_TOC_ID, false, dirname(plugin_basename(__FILE__)) . '/locale/');
} }
public function shortcode_extoc($atts) { public function shortcode_extoc($atts) {
extract( shortcode_atts( array( extract(shortcode_atts(array(
'start' => $this->options["start"], 'start' => $this->options["start"],
'headers' => $this->options["heading_levels"], 'headers' => $this->options["heading_levels"],
'title' => $this->options["heading_text"], 'title' => $this->options["heading_text"],
), $atts ) ); ), $atts));
if( !is_array($headers) ) if (!is_array($headers))
$headers = preg_split('/[\s*,]+/i', $headers); $headers = preg_split('/[\s*,]+/i', $headers);
if($start) $this->options['start'] = $start; if ($start)
if($headers) $this->options['heading_levels'] = $headers; $this->options['start'] = $start;
if($title) $this->options['heading_text'] = $title; if ($headers)
$this->options['heading_levels'] = $headers;
if ($title)
$this->options['heading_text'] = $title;
if( isset($atts[0]['notitle']) ) if (isset($atts[0]['notitle']))
$this->options['show_heading_text'] = false; $this->options['show_heading_text'] = false;
if( !is_search() && !is_archive() && !is_feed() && !is_front_page() ) if (!is_search() && !is_archive() && !is_feed() && !is_front_page())
return '[extoc]'; return '[extoc]';
else else
return; return;
@ -334,23 +337,23 @@ if( !class_exists('ExToC') ) {
// Reset the counter // Reset the counter
$this->counter = array(); $this->counter = array();
if( is_search() || is_archive() || is_front_page() || is_feed() ) if (is_search() || is_archive() || is_front_page() || is_feed())
return $content; return $content;
/** Extract the content, and extract the part content if <!--nextpage--> was used **/ /** Extract the content, and extract the part content if <!--nextpage--> was used * */
$this->content = $content; // The original content (subpage) that is displayed $this->content = $content; // The original content (subpage) that is displayed
$this->extract_full_post_content(); $this->extract_full_post_content();
$toc_content = "<div id=\"toc-np-container\">"; $toc_content = "<div id=\"toc-np-container\">";
if( $this->options['show_heading_text'] == true ) if ($this->options['show_heading_text'] == true)
$toc_content .= "<p id=\"toc-np-title\">" . $this->options["heading_text"] . "</p>"; $toc_content .= "<p id=\"toc-np-title\">" . $this->options["heading_text"] . "</p>";
$toc_content .= "<ul class=\"no-bullets\">"; $toc_content .= "<ul class=\"no-bullets\">";
$toc_content .= $this->extract_toc(); $toc_content .= $this->extract_toc();
$toc_content .= "</ul></div>"; $toc_content .= "</ul></div>";
if( $this->totalHeadings >= $this->options['start'] ) if ($this->totalHeadings >= $this->options['start'])
return $this->insert_toc_at_markup_position($toc_content); // $toc_content . $this->content; return $this->insert_toc_at_markup_position($toc_content); // $toc_content . $this->content;
else { else {
$content = preg_replace("/\[extoc\]|\[noextoc\]/", "", $this->content); $content = preg_replace("/\[extoc\]|\[noextoc\]/", "", $this->content);
@ -360,32 +363,29 @@ if( !class_exists('ExToC') ) {
} }
/** returns the content for display added by the ToC */ /** returns the content for display added by the ToC */
private function insert_toc_at_markup_position($toc_content) private function insert_toc_at_markup_position($toc_content) {
{
// clean content without markups for returning // clean content without markups for returning
$content = $this->content; $content = $this->content;
$content = preg_replace("/\[noextoc\]/", "", $content); // preg_replace("/\[extoc\]|\[noextoc\]/", "", $content); $content = preg_replace("/\[noextoc\]/", "", $content); // preg_replace("/\[extoc\]|\[noextoc\]/", "", $content);
// [noextoc] has priority. If this is found, return the original // [noextoc] has priority. If this is found, return the original
if( strpos($this->content, '[noextoc]') !== false ) if (strpos($this->content, '[noextoc]') !== false)
return $content; return $content;
// try to find the markup for the ToC // try to find the markup for the ToC
$pos = strpos($this->content, '[extoc]'); $pos = strpos($this->content, '[extoc]');
// we didn't find any markup... // we didn't find any markup...
if( $pos === false ) { if ($pos === false) {
global $post; global $post;
// There was no markup, so insert at top or return original if this type does not need a ToC // There was no markup, so insert at top or return original if this type does not need a ToC
if( !in_array(get_post_type($post), $this->options['auto_insert_post_types']) ) if (!in_array(get_post_type($post), $this->options['auto_insert_post_types']))
return $content; return $content;
else else
return $toc_content . $content; return $toc_content . $content;
} }
// In this case, the markup was found in the content // In this case, the markup was found in the content
if( is_numeric($pos) && $pos >= 0 ) if (is_numeric($pos) && $pos >= 0) {
{
return str_replace('[extoc]', $toc_content, $content); // substr($content, 0, $pos) . $toc_content . substr($content, $pos); return str_replace('[extoc]', $toc_content, $content); // substr($content, 0, $pos) . $toc_content . substr($content, $pos);
} }
@ -393,7 +393,7 @@ if( !class_exists('ExToC') ) {
return $content; return $content;
} }
/** Extract the full unshortened content from the post **/ /** Extract the full unshortened content from the post * */
private function extract_full_post_content() { private function extract_full_post_content() {
global $post; global $post;
$this->fullcontent = $post->post_content; $this->fullcontent = $post->post_content;
@ -419,7 +419,7 @@ if( !class_exists('ExToC') ) {
// private $minLevel = null; // private $minLevel = null;
/** Extract headings from every pages */ /** Extract headings from every pages */
for( $pagenum = 1; $pagenum <= count($this->pages); $pagenum++ ) { for ($pagenum = 1; $pagenum <= count($this->pages); $pagenum++) {
$headers .= $this->exctract_headings($pagenum); $headers .= $this->exctract_headings($pagenum);
} }
@ -432,18 +432,18 @@ if( !class_exists('ExToC') ) {
} }
private function exctract_headings($pagenum) { private function exctract_headings($pagenum) {
/** find all header tags within the page **/ /** find all header tags within the page * */
preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $this->pages[$pagenum-1], $matches, PREG_SET_ORDER); preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $this->pages[$pagenum - 1], $matches, PREG_SET_ORDER);
if (count($matches) == 0) { if (count($matches) == 0) {
return null; return null;
} }
/** Check the headings that are desired */ /** Check the headings that are desired */
if( count($this->options['heading_levels']) != 6 ) { if (count($this->options['heading_levels']) != 6) {
$new_matches = array(); $new_matches = array();
for ($i = 0; $i < count($matches); $i++) { for ($i = 0; $i < count($matches); $i++) {
if( in_array($matches[$i][2], $this->options['heading_levels']) ) if (in_array($matches[$i][2], $this->options['heading_levels']))
$new_matches[] = $matches[$i]; $new_matches[] = $matches[$i];
} }
$matches = $new_matches; $matches = $new_matches;
@ -452,14 +452,14 @@ if( !class_exists('ExToC') ) {
$items = ""; $items = "";
/** Take first h-level as baseline */ /** Take first h-level as baseline */
if( $this->minLevel == null ) if ($this->minLevel == null)
$this->minLevel = $matches[0][2]; // lowest level e.g. h3 $this->minLevel = $matches[0][2]; // lowest level e.g. h3
$currentLevel = $this->minLevel; // $minLevel; $currentLevel = $this->minLevel; // $minLevel;
$this->counter[$currentLevel] = 0; $this->counter[$currentLevel] = 0;
for( $i = 0; $i < count($matches); $i++ ) { for ($i = 0; $i < count($matches); $i++) {
/** get anchor and add to find and replace arrays **/ /** get anchor and add to find and replace arrays * */
$anchor = $this->url_encode_anchor($matches[$i][0]); $anchor = $this->url_encode_anchor($matches[$i][0]);
$find = $matches[$i][0]; $find = $matches[$i][0];
$replace = str_replace( $replace = str_replace(
@ -477,15 +477,13 @@ if( !class_exists('ExToC') ) {
$this->content = str_replace($find, $replace, $this->content); $this->content = str_replace($find, $replace, $this->content);
/** Check if header lower current header, then add level and update current header */ /** Check if header lower current header, then add level and update current header */
if( $matches[$i][2] > $currentLevel && $this->options['show_hierarchy'] == true) { if ($matches[$i][2] > $currentLevel && $this->options['show_hierarchy'] == true) {
$currentLevel = $matches[$i][2]; $currentLevel = $matches[$i][2];
$this->counter[$currentLevel] += 1; $this->counter[$currentLevel] += 1;
} } else if ($matches[$i][2] < $currentLevel && $matches[$i][2] >= $this->minLevel && $this->options['show_hierarchy'] == true) {
else if( $matches[$i][2] < $currentLevel && $matches[$i][2] >= $this->minLevel && $this->options['show_hierarchy'] == true) {
$currentLevel = $matches[$i][2]; $currentLevel = $matches[$i][2];
$this->counter[$currentLevel] += 1; $this->counter[$currentLevel] += 1;
} } else {
else {
$this->counter[$currentLevel] += 1; $this->counter[$currentLevel] += 1;
} }
@ -493,21 +491,21 @@ if( !class_exists('ExToC') ) {
$items .= '<li class="header-level-' . ($currentLevel - $this->minLevel + 1) . '">'; $items .= '<li class="header-level-' . ($currentLevel - $this->minLevel + 1) . '">';
global $page; global $page;
if( $pagenum == $page && is_single() ) if ($pagenum == $page && is_single())
$items .= '<a href="#' . $anchor . '">'; $items .= '<a href="#' . $anchor . '">';
else { else {
if( $pagenum == 1 ) if ($pagenum == 1)
$items .= '<a href="' . get_permalink($this->ID) . '#' . $anchor . '">'; $items .= '<a href="' . get_permalink($this->ID) . '#' . $anchor . '">';
else else
$items .= '<a href="?p='.$this->ID.($pagenum>1?'&page='.$pagenum:'').'#' . $anchor . '">'; $items .= '<a href="?p=' . $this->ID . ($pagenum > 1 ? '&page=' . $pagenum : '') . '#' . $anchor . '">';
} }
// Show numbers only if user wants it // Show numbers only if user wants it
if( $this->options['number_list_items'] ) { if ($this->options['number_list_items']) {
$items .= "<span class=\"toc-np-number\">"; $items .= "<span class=\"toc-np-number\">";
if( $this->options['show_hierarchy'] == true ) { if ($this->options['show_hierarchy'] == true) {
for( $j = $this->minLevel; $j < $currentLevel; $j++ ) { for ($j = $this->minLevel; $j < $currentLevel; $j++) {
$items = $items . $this->counter[$j] . "."; $items = $items . $this->counter[$j] . ".";
} }
} }
@ -525,18 +523,17 @@ if( !class_exists('ExToC') ) {
return $items; return $items;
} }
private function url_encode_anchor($anchor) private function url_encode_anchor($anchor) {
{
$return = false; $return = false;
if(!empty($anchor) ) { if (!empty($anchor)) {
/** Remove tags */ /** Remove tags */
$return = trim( strip_tags($anchor) ); $return = trim(strip_tags($anchor));
/** remove &amp; */ /** remove &amp; */
$return = str_replace( '&amp;', '', $return ); $return = str_replace('&amp;', '', $return);
/** remove all unknown chars **/ /** remove all unknown chars * */
$return = preg_replace("/[^0-9a-zA-Z \-_]+/", "", $return); $return = preg_replace("/[^0-9a-zA-Z \-_]+/", "", $return);
/** Remove backspace etc */ /** Remove backspace etc */
@ -549,10 +546,11 @@ if( !class_exists('ExToC') ) {
return $return; return $return;
} }
} }
} }
/** Initialise the class */ /** Initialise the class */
$tocPlugin = new ExToC(); $tocPlugin = new ExToC();
?> ?>