Advertisement
Guest User

Untitled

a guest
Dec 12th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.41 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Excerpts - Simply add pages excerpts in your pages.
  4.  *
  5.  * @version 0.2.1
  6.  * @author Wojciech "yojoe" Jodła
  7.  * @link http://jodla.ayz.pl
  8.  * @author Nicolas "p01" Liautaud
  9.  * @link http://nliautaud.fr
  10.  * @author Denis "Wakh" Sharov
  11.  * @link http://webdev.wakh.ru/
  12.  */
  13.  
  14. # register plugin
  15. $thisfile=basename(__FILE__, ".php");
  16. register_plugin(
  17.     $thisfile,      # ID of plugin, should be filename minus php
  18.     'Excerpts 2',       # Title of plugin
  19.     '0.3',      # Version of plugin
  20.     'yojoe + p01 + Wakh',   # Author of plugin
  21.     'http://jodla.ayz.pl',  # Author URL
  22.     'Simply add pages excerpts in your pages.', # Page type of plugin
  23.     ''      # Function that displays content
  24. );
  25.  
  26. add_filter('content','parse_tags');
  27.  
  28. /* Parse a string and replace tags by pages excerpts
  29.  * @param string $contents the string to parse
  30.  * @return string the modified string
  31.  */
  32. function parse_tags($contents)
  33. {
  34.   $pattern = '`(?<!<code>)\(%\s*excerpt\s*:\s*([^, ]*)\s*';
  35.   $pattern.= '(?:,\s*([0-9]*)\s*)?'; // chars number
  36.   $pattern.= '(?:,\s*([0-9]*)\s*)?'; // chars number
  37.   $pattern.= '(?:,\s*(text|html)\s*)'; // text or html
  38.   $pattern.= '(?:,\s*(link|nolink)\s*)'; // readmore
  39.   $pattern.= '?%\)`';
  40.   return preg_replace_callback($pattern, 'callback_to_excerpt', $contents);
  41. }
  42.  
  43. /* Call page_excerpt from expreg with good parameters.
  44.  * @param string $mask the pattern result
  45.  * @return string the page excerpt
  46.  */
  47. function callback_to_excerpt($mask)
  48. {
  49.   $mask['2'] = isset($mask['2']) ? $mask['2'] : 0;
  50.   $mask['3'] = isset($mask['3']) ? $mask['3'] : 200;
  51.   $mask['4'] = isset($mask['4']) ? $mask['4'] : 'text';
  52.   $mask['5'] = isset($mask['5']) ? $mask['5'] : 'link';
  53.   return page_excerpt($mask['1'], $mask['2'], $mask['3'], $mask['4'], $mask['5']);
  54. }
  55.  
  56. /* Return the excerpt of a defined page.
  57.  * @param string $name the page name
  58.  * @param int $chars the excerpt length (200 by default)
  59.  * @param string $type output type : text or html (text by default)
  60.  * @return string the excerpt
  61.  */
  62. function page_excerpt($name, $offset = 0, $chars = 200, $type = 'text', $link = 'link')
  63. {
  64.   $file_url = GSDATAPAGESPATH . $name . '.xml';
  65.   if(file_exists($file_url))
  66.   {
  67.     $file = file_get_contents($file_url);
  68.     $page = simplexml_load_string($file);
  69.     $page_len = strlen($page->content);
  70.     $excerpt = $page->content;
  71.    
  72.     if($type == 'text') {
  73.       $excerpt = htmlspecialchars_decode($excerpt);
  74.       $excerpt = trim(strip_html_tags($excerpt));
  75.     } else {
  76.       $excerpt = html_entity_decode($excerpt);
  77.     }
  78.     $excerpt = mb_substr($excerpt, $offset, $chars, 'UTF-8');
  79.     if($page_len > $chars) {
  80.         if($link == 'link') {
  81.       $excerpt .= '... <a class="read-more" href="' . $name . '/">Подробнее</a>';
  82.             }  
  83.     }
  84.     return '<p>' . $excerpt . '</p>';
  85.   }
  86.   return '<p>Plugin error: target page not found.</p>';
  87. }
  88.  
  89.  
  90. /* Strip html tags and remove invisible html tags content.
  91.  *
  92.  * PHP's strip_tags() function will remove tags, but it
  93.  * doesn't remove scripts, styles, and other unwanted
  94.  * invisible text between tags.  Also, as a prelude to
  95.  * tokenizing the text, we need to insure that when
  96.  * block-level tags (such as <p> or <div>) are removed,
  97.  * neighboring words aren't joined.
  98. */
  99. function strip_html_tags($text)
  100. {
  101.   $text = preg_replace(
  102.     array(
  103.       // Remove invisible content
  104.       '@<head[^>]*?>.*?</head>@siu',
  105.       '@<style[^>]*?>.*?</style>@siu',
  106.       '@<script[^>]*?.*?</script>@siu',
  107.       '@<object[^>]*?.*?</object>@siu',
  108.       '@<embed[^>]*?.*?</embed>@siu',
  109.       '@<applet[^>]*?.*?</applet>@siu',
  110.       '@<noframes[^>]*?.*?</noframes>@siu',
  111.       '@<noscript[^>]*?.*?</noscript>@siu',
  112.       '@<noembed[^>]*?.*?</noembed>@siu',
  113.  
  114.       // Add line breaks before & after blocks
  115.       '@<((br)|(hr))@iu',
  116.       '@</?((address)|(blockquote)|(center)|(del))@iu',
  117.       '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
  118.       '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
  119.       '@</?((table)|(th)|(td)|(caption))@iu',
  120.       '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
  121.       '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
  122.       '@</?((frameset)|(frame)|(iframe))@iu',
  123.     ),
  124.     array(
  125.       ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  126.       "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
  127.       "\n\$0", "\n\$0",
  128.     ),
  129.     $text);
  130.  
  131.   // Remove all remaining tags and comments and return.
  132.   return strip_tags($text);
  133. }
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement