Plugin Directory

Changeset 1338224


Ignore:
Timestamp:
01/28/2016 03:34:36 PM (10 years ago)
Author:
JasWSInc
Message:

Releasing v160128

Location:
ezphp
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • ezphp/trunk/ezphp.php

    r1089801 r1338224  
    11<?php
    22/*
    3 Version: 150214
     3Version: 160128
    44Text Domain: ezphp
    55Plugin Name: ezPHP
    66
    7 Author URI: http://www.websharks-inc.com/
     7Author URI: http://websharks-inc.com/
    88Author: WebSharks, Inc. (Jason Caldwell)
    99
     
    1616if(!defined('EZPHP_INCLUDED_POST_TYPES')) define('EZPHP_INCLUDED_POST_TYPES', '');
    1717if(!defined('EZPHP_EXCLUDED_POST_TYPES')) define('EZPHP_EXCLUDED_POST_TYPES', '');
     18if(!defined('EZPHP_STRIP_MD_INDENTS')) define('EZPHP_STRIP_MD_INDENTS', FALSE);
    1819
    1920class ezphp // PHP execution plugin for WordPress.
    2021{
    21     public static $included_post_types = array(); // Inclusions array.
    22     public static $excluded_post_types = array(); // Exclusions array.
    23 
    24     public static function init() // Initialize plugin :-)
    25     {
    26         #load_plugin_textdomain('ezphp'); // Not necessary at this time.
    27 
     22    public static $included_post_types = array();
     23    public static $excluded_post_types = array();
     24    public static $strip_md_indents = FALSE;
     25
     26    public static function init() // Initialize plugin.
     27    {
    2828        if(EZPHP_INCLUDED_POST_TYPES) // Specific Post Types?
    2929            ezphp::$included_post_types = // Convert these to an array.
     
    3636        ezphp::$excluded_post_types = apply_filters('ezphp_excluded_post_types', ezphp::$excluded_post_types);
    3737
     38        if(EZPHP_STRIP_MD_INDENTS) ezphp::$strip_md_indents = TRUE;
     39        ezphp::$strip_md_indents = apply_filters('ezphp_strip_md_indents', ezphp::$strip_md_indents);
     40
    3841        add_filter('the_content', 'ezphp::filter', 1);
    3942        add_filter('get_the_excerpt', 'ezphp::filter', 1);
    40         add_filter('widget_text', 'ezphp::evaluate', 1);
     43        add_filter('widget_text', 'ezphp::maybe_eval', 1);
    4144    }
    4245
    4346    public static function filter($content_excerpt)
    4447    {
    45         $post_type = get_post_type();
    46 
    47         if($post_type && ezphp::$included_post_types) // Specific inclusions?
    48             if(!in_array($post_type, ezphp::$included_post_types, TRUE))
     48        $post = get_post(); // Current post.
     49
     50        if($post && $post->post_type && ezphp::$included_post_types)
     51            if(!in_array($post->post_type, ezphp::$included_post_types, TRUE))
    4952                return $content_excerpt; // Exclude.
    5053
    51         if($post_type && ezphp::$excluded_post_types) // Specific exclusions?
    52             if(in_array($post_type, ezphp::$excluded_post_types, TRUE))
     54        if($post && $post->post_type && ezphp::$excluded_post_types)
     55            if(in_array($post->post_type, ezphp::$excluded_post_types, TRUE))
    5356                return $content_excerpt; // Exclude.
    5457
    55         return ezphp::evaluate($content_excerpt);
    56     }
    57 
    58     public static function evaluate($string)
    59     {
    60         if(!$string || stripos($string, 'php') === FALSE)
     58        if($post && apply_filters('ezphp_exclude_post', FALSE, $post))
     59            return $content_excerpt; // Exclude.
     60
     61        return ezphp::maybe_eval($content_excerpt);
     62    }
     63
     64    public static function maybe_eval($string)
     65    {
     66        if(!($string = (string)$string))
     67            return $string; // Empty.
     68
     69        if(stripos($string, 'php') === FALSE)
    6170            return $string; // Saves time.
    6271
     
    6574
    6675        if(stripos($string, '< ?php') !== FALSE) // WP `force_balance_tags()` does this.
    67             $string = str_ireplace('< ?php', '<?php ', $string); // Quick fix.
     76            $string = str_ireplace('< ?php', '<?php ', $string); // Quick fix here.
     77
     78        if(!preg_match('/\<\?php\s/i', $string)) // String contains PHP tags?
     79            return ezphp::convert_excl_tags($string); // Nothing to evaluate.
    6880
    6981        ob_start(); // Output buffer PHP code execution to collect echo/print calls.
     
    7183        $string = ob_get_clean(); // Collect output buffer.
    7284
    73         if(stripos($string, '!php') !== FALSE) // PHP code samples; e.g. <!php !> tags.
    74             $string = preg_replace(array('/\< ?\!php(\s+)/i', '/(\s+)\!\>/'), array('<?php${1}', '${1}?>'), $string);
    75 
    76         return $string; // All done :-)
     85        return ezphp::maybe_strip_md_indents(ezphp::convert_excl_tags($string));
     86    }
     87
     88    public static function convert_excl_tags($string)
     89    {
     90        if(!($string = (string)$string))
     91            return $string; // Empty.
     92
     93        if(stripos($string, '!php') === FALSE)
     94            return $string; // Saves time.
     95
     96        return preg_replace(array('/\< ?\!php(\s)/i', '/(\s)\!\>/'), array('<?php${1}', '${1}?>'), $string);
     97    }
     98
     99    public static function maybe_strip_md_indents($string)
     100    {
     101        if(!ezphp::$strip_md_indents)
     102            return $string; // Not applicable.
     103
     104        if(!($string = (string)$string))
     105            return $string; // Empty.
     106
     107        if(strpos($string, '    ') === FALSE && strpos($string, "\t") === FALSE)
     108            return $string; // Nothing to strip.
     109
     110        $spcsm           = ezphp::spcsm_tokens($string, array('shortcodes', 'pre', 'md_fences'), __FUNCTION__);
     111        $spcsm['string'] = preg_replace('/^(?: {4,}|'."\t".'+)/m', '', $spcsm['string']);
     112        $string          = ezphp::spcsm_restore($spcsm);
     113
     114        return $string; // All done.
     115    }
     116
     117    public static function spcsm_tokens($string, array $tokenize_only = array(), $marker = '')
     118    {
     119        $marker = str_replace('.', '', uniqid('', TRUE)).($marker ? sha1($marker) : '');
     120
     121        if(!($string = (string)$string)) // Nothing to tokenize.
     122            return array('string' => $string, 'tokens' => array(), 'marker' => $marker);
     123
     124        $spcsm = // Convert string to an array w/ token details.
     125            array('string' => $string, 'tokens' => array(), 'marker' => $marker);
     126
     127        shortcodes: // Target point; `[shortcode][/shortcode]`.
     128
     129        if($tokenize_only && !in_array('shortcodes', $tokenize_only, TRUE))
     130            goto pre; // Not tokenizing these.
     131
     132        if(empty($GLOBALS['shortcode_tags']) || strpos($spcsm['string'], '[') === FALSE)
     133            goto pre; // No `[` shortcodes.
     134
     135        $spcsm['string'] = preg_replace_callback('/'.get_shortcode_regex().'/s', function ($m) use (&$spcsm)
     136        {
     137            $spcsm['tokens'][] = $m[0]; // Tokenize.
     138            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     139
     140        }, $spcsm['string']); // Shortcodes replaced by tokens.
     141
     142        pre: // Target point; HTML `<pre>` tags.
     143
     144        if($tokenize_only && !in_array('pre', $tokenize_only, TRUE))
     145            goto code; // Not tokenizing these.
     146
     147        if(stripos($spcsm['string'], '<pre') === FALSE)
     148            goto code; // Nothing to tokenize here.
     149
     150        $pre = // HTML `<pre>` tags.
     151            '/(?P<tag_open_bracket>\<)'. // Opening `<` bracket.
     152            '(?P<tag_open_name>pre)'. // Tag name; e.g. a `pre` tag.
     153            '(?P<tag_open_attrs_bracket>\>|\s+[^>]*\>)'. // Attributes & `>`.
     154            '(?P<tag_contents>.*?)'. // Tag contents (multiline possible).
     155            '(?P<tag_close>\<\/\\2\>)/is'; // e.g. closing `</pre>` tag.
     156
     157        $spcsm['string'] = preg_replace_callback($pre, function ($m) use (&$spcsm)
     158        {
     159            $spcsm['tokens'][] = $m[0]; // Tokenize.
     160            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     161
     162        }, $spcsm['string']); // Tags replaced by tokens.
     163
     164        code: // Target point; HTML `<code>` tags.
     165
     166        if($tokenize_only && !in_array('code', $tokenize_only, TRUE))
     167            goto samp; // Not tokenizing these.
     168
     169        if(stripos($spcsm['string'], '<code') === FALSE)
     170            goto samp; // Nothing to tokenize here.
     171
     172        $code = // HTML `<code>` tags.
     173            '/(?P<tag_open_bracket>\<)'. // Opening `<` bracket.
     174            '(?P<tag_open_name>code)'. // Tag name; e.g. a `code` tag.
     175            '(?P<tag_open_attrs_bracket>\>|\s+[^>]*\>)'. // Attributes & `>`.
     176            '(?P<tag_contents>.*?)'. // Tag contents (multiline possible).
     177            '(?P<tag_close>\<\/\\2\>)/is'; // e.g. closing `</code>` tag.
     178
     179        $spcsm['string'] = preg_replace_callback($code, function ($m) use (&$spcsm)
     180        {
     181            $spcsm['tokens'][] = $m[0]; // Tokenize.
     182            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     183
     184        }, $spcsm['string']); // Tags replaced by tokens.
     185
     186        samp: // Target point; HTML `<samp>` tags.
     187
     188        if($tokenize_only && !in_array('samp', $tokenize_only, TRUE))
     189            goto md_fences; // Not tokenizing these.
     190
     191        if(stripos($spcsm['string'], '<samp') === FALSE)
     192            goto md_fences; // Nothing to tokenize here.
     193
     194        $samp = // HTML `<samp>` tags.
     195            '/(?P<tag_open_bracket>\<)'. // Opening `<` bracket.
     196            '(?P<tag_open_name>samp)'. // Tag name; e.g. a `samp` tag.
     197            '(?P<tag_open_attrs_bracket>\>|\s+[^>]*\>)'. // Attributes & `>`.
     198            '(?P<tag_contents>.*?)'. // Tag contents (multiline possible).
     199            '(?P<tag_close>\<\/\\2\>)/is'; // e.g. closing `</samp>` tag.
     200
     201        $spcsm['string'] = preg_replace_callback($samp, function ($m) use (&$spcsm)
     202        {
     203            $spcsm['tokens'][] = $m[0]; // Tokenize.
     204            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     205
     206        }, $spcsm['string']); // Tags replaced by tokens.
     207
     208        md_fences: // Target point; Markdown pre/code fences.
     209
     210        if($tokenize_only && !in_array('md_fences', $tokenize_only, TRUE))
     211            goto md_links; // Not tokenizing these.
     212
     213        if(strpos($spcsm['string'], '~') === FALSE && strpos($spcsm['string'], '`') === FALSE)
     214            goto md_links; // Nothing to tokenize here.
     215
     216        $md_fences = // Markdown pre/code fences.
     217            '/(?P<fence_open>~{3,}|`{3,}|`)'. // Opening fence.
     218            '(?P<fence_contents>.*?)'. // Contents (multiline possible).
     219            '(?P<fence_close>\\1)/is'; // Closing fence; ~~~, ```, `.
     220
     221        $spcsm['string'] = preg_replace_callback($md_fences, function ($m) use (&$spcsm)
     222        {
     223            $spcsm['tokens'][] = $m[0]; // Tokenize.
     224            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     225
     226        }, $spcsm['string']); // Fences replaced by tokens.
     227
     228        md_links: // Target point; [Markdown](links).
     229        // This also tokenizes [Markdown]: <link> "definitions".
     230        // This routine includes considerations for images also.
     231
     232        // NOTE: The tokenizer does NOT deal with links that reference definitions, as this is not necessary.
     233        //    So, while we DO tokenize <link> "definitions" themselves, the [actual][references] to
     234        //    these definitions do not need to be tokenized; i.e. it is not necessary here.
     235
     236        if($tokenize_only && !in_array('md_links', $tokenize_only, TRUE))
     237            goto finale; // Not tokenizing these.
     238
     239        $spcsm['string'] = preg_replace_callback(array('/^[ ]*(?:\[[^\]]+\])+[ ]*\:[ ]*(?:\<[^>]+\>|\S+)(?:[ ]+.+)?$/m',
     240                                                       '/\!?\[(?:(?R)|[^\]]*)\]\([^)]+\)(?:\{[^}]*\})?/'), function ($m) use (&$spcsm)
     241        {
     242            $spcsm['tokens'][] = $m[0]; // Tokenize.
     243            return '%#%spcsm-'.$spcsm['marker'].'-'.(count($spcsm['tokens']) - 1).'%#%'; #
     244
     245        }, $spcsm['string']); // Shortcodes replaced by tokens.
     246
     247        finale: // Target point; grand finale (return).
     248
     249        return $spcsm; // Array w/ string, tokens, and marker.
     250    }
     251
     252    public static function spcsm_restore(array $spcsm)
     253    {
     254        if(!isset($spcsm['string']))
     255            return ''; // Not possible.
     256
     257        if(!($string = (string)$spcsm['string']))
     258            return $string; // Nothing to restore.
     259
     260        $tokens = isset($spcsm['tokens']) ? (array)$spcsm['tokens'] : array();
     261        $marker = isset($spcsm['marker']) ? (string)$spcsm['marker'] : '';
     262
     263        if(!$tokens || !$marker || strpos($string, '%#%') === FALSE)
     264            return $string; // Nothing to restore in this case.
     265
     266        foreach(array_reverse($tokens, TRUE) as $_token => $_value)
     267            $string = str_replace('%#%spcsm-'.$marker.'-'.$_token.'%#%', $_value, $string);
     268        // Must go in reverse order so nested tokens unfold properly.
     269        unset($_token, $_value); // Housekeeping.
     270
     271        return $string; // Restoration complete.
    77272    }
    78273
  • ezphp/trunk/readme.txt

    r1089801 r1338224  
    11=== ezPHP for WordPress ===
    22
    3 Stable tag: 150214
     3Stable tag: 160128
    44Requires at least: 3.3
    5 Tested up to: 4.2-alpha
     5Tested up to: 4.5-alpha
    66Text Domain: ezphp
    77
     
    8989== Changelog ==
    9090
     91= v160128 =
     92
     93- Improving support for PHP mixed with Markdown for themes that use Markdown.
     94
    9195= v150214 =
    9296
Note: See TracChangeset for help on using the changeset viewer.