Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Excerpts - Simply add pages excerpts in your pages.
- *
- * @version 0.2.1
- * @author Wojciech "yojoe" Jodła
- * @link http://jodla.ayz.pl
- * @author Nicolas "p01" Liautaud
- * @link http://nliautaud.fr
- * @author Denis "Wakh" Sharov
- * @link http://webdev.wakh.ru/
- */
- # register plugin
- $thisfile=basename(__FILE__, ".php");
- register_plugin(
- $thisfile, # ID of plugin, should be filename minus php
- 'Excerpts 2', # Title of plugin
- '0.3', # Version of plugin
- 'yojoe + p01 + Wakh', # Author of plugin
- 'http://jodla.ayz.pl', # Author URL
- 'Simply add pages excerpts in your pages.', # Page type of plugin
- '' # Function that displays content
- );
- add_filter('content','parse_tags');
- /* Parse a string and replace tags by pages excerpts
- * @param string $contents the string to parse
- * @return string the modified string
- */
- function parse_tags($contents)
- {
- $pattern = '`(?<!<code>)\(%\s*excerpt\s*:\s*([^, ]*)\s*';
- $pattern.= '(?:,\s*([0-9]*)\s*)?'; // chars number
- $pattern.= '(?:,\s*([0-9]*)\s*)?'; // chars number
- $pattern.= '(?:,\s*(text|html)\s*)'; // text or html
- $pattern.= '(?:,\s*(link|nolink)\s*)'; // readmore
- $pattern.= '?%\)`';
- return preg_replace_callback($pattern, 'callback_to_excerpt', $contents);
- }
- /* Call page_excerpt from expreg with good parameters.
- * @param string $mask the pattern result
- * @return string the page excerpt
- */
- function callback_to_excerpt($mask)
- {
- $mask['2'] = isset($mask['2']) ? $mask['2'] : 0;
- $mask['3'] = isset($mask['3']) ? $mask['3'] : 200;
- $mask['4'] = isset($mask['4']) ? $mask['4'] : 'text';
- $mask['5'] = isset($mask['5']) ? $mask['5'] : 'link';
- return page_excerpt($mask['1'], $mask['2'], $mask['3'], $mask['4'], $mask['5']);
- }
- /* Return the excerpt of a defined page.
- * @param string $name the page name
- * @param int $chars the excerpt length (200 by default)
- * @param string $type output type : text or html (text by default)
- * @return string the excerpt
- */
- function page_excerpt($name, $offset = 0, $chars = 200, $type = 'text', $link = 'link')
- {
- $file_url = GSDATAPAGESPATH . $name . '.xml';
- if(file_exists($file_url))
- {
- $file = file_get_contents($file_url);
- $page = simplexml_load_string($file);
- $page_len = strlen($page->content);
- $excerpt = $page->content;
- if($type == 'text') {
- $excerpt = htmlspecialchars_decode($excerpt);
- $excerpt = trim(strip_html_tags($excerpt));
- } else {
- $excerpt = html_entity_decode($excerpt);
- }
- $excerpt = mb_substr($excerpt, $offset, $chars, 'UTF-8');
- if($page_len > $chars) {
- if($link == 'link') {
- $excerpt .= '... <a class="read-more" href="' . $name . '/">Подробнее</a>';
- }
- }
- return '<p>' . $excerpt . '</p>';
- }
- return '<p>Plugin error: target page not found.</p>';
- }
- /* Strip html tags and remove invisible html tags content.
- *
- * PHP's strip_tags() function will remove tags, but it
- * doesn't remove scripts, styles, and other unwanted
- * invisible text between tags. Also, as a prelude to
- * tokenizing the text, we need to insure that when
- * block-level tags (such as <p> or <div>) are removed,
- * neighboring words aren't joined.
- */
- function strip_html_tags($text)
- {
- $text = preg_replace(
- array(
- // Remove invisible content
- '@<head[^>]*?>.*?</head>@siu',
- '@<style[^>]*?>.*?</style>@siu',
- '@<script[^>]*?.*?</script>@siu',
- '@<object[^>]*?.*?</object>@siu',
- '@<embed[^>]*?.*?</embed>@siu',
- '@<applet[^>]*?.*?</applet>@siu',
- '@<noframes[^>]*?.*?</noframes>@siu',
- '@<noscript[^>]*?.*?</noscript>@siu',
- '@<noembed[^>]*?.*?</noembed>@siu',
- // Add line breaks before & after blocks
- '@<((br)|(hr))@iu',
- '@</?((address)|(blockquote)|(center)|(del))@iu',
- '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
- '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
- '@</?((table)|(th)|(td)|(caption))@iu',
- '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
- '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
- '@</?((frameset)|(frame)|(iframe))@iu',
- ),
- array(
- ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
- "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
- "\n\$0", "\n\$0",
- ),
- $text);
- // Remove all remaining tags and comments and return.
- return strip_tags($text);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement