Plugin Directory

Changeset 1081302


Ignore:
Timestamp:
02/03/2015 02:05:43 AM (11 years ago)
Author:
JasWSInc
Message:

Releasing version 150201.

Location:
wp-snippets
Files:
11 added
2 edited

Legend:

Unmodified
Added
Removed
  • wp-snippets/trunk/readme.txt

    r808243 r1081302  
    11=== WP Snippets ===
    22
    3 Stable tag: 131121
    4 Requires at least: 3.2
    5 Tested up to: 3.7.1
     3Stable tag: 150201
     4Requires at least: 3.3
     5Tested up to: 4.2-alpha
    66Text Domain: wp-snippets
    77
     
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    1010
    11 Contributors: WebSharks
     11Contributors: WebSharks, JasWSInc, raamdev, bruce-caldwell
    1212Donate link: http://www.websharks-inc.com/r/wp-theme-plugin-donation/
    1313Tags: snippet, snippets, include, includes, shortcode, shortcodes, php, post type, post types, utilities, posts, pages
     
    114114== Changelog ==
    115115
     116= v150201 =
     117
     118* Reformat source code; general cleanup.
     119* Compatibility with WordPress v4.1 and v4.2-alpha.
     120* Bug fix in hook/filter handling. See: [this GitHub issue](https://github.com/websharks/wp-snippets/pull/2) for further details.
     121
    116122= v131121 =
    117123
  • wp-snippets/trunk/wp-snippets.php

    r808243 r1081302  
    11<?php
    22/*
    3 Version: 131121
     3Version: 150201
    44Text Domain: wp-snippets
    55Plugin Name: WP Snippets
    66
     7Author: WebSharks, Inc. (Jason Caldwell)
    78Author URI: http://www.websharks-inc.com/
    8 Author: WebSharks, Inc. (Jason Caldwell)
    99
    1010Plugin URI: http://www.websharks-inc.com/product/wp-snippets/
     
    1313if(!defined('WPINC')) // MUST have WordPress.
    1414    exit('Do NOT access this file directly: '.basename(__FILE__));
    15 
    16 if(!defined('WP_SNIPPET_ROLES_ALL_CAPS')) define('WP_SNIPPET_ROLES_ALL_CAPS', 'administrator');
    17 if(!defined('WP_SNIPPET_ROLES_EDIT_CAPS')) define('WP_SNIPPET_ROLES_EDIT_CAPS', 'administrator,editor,author');
    18 
    19 class wp_snippets // WP Snippets; like PHP includes for WordPress.
     15/* ------------------------------------------------------------------------- */
     16
     17/**
     18 * WP Snippets class.
     19 */
     20class wp_snippets // Like PHP includes; for WordPress.
    2021{
    21     public static $roles_all_caps = array(); // WP Roles; as array.
    22     public static $roles_edit_caps = array(); // WP Roles; as array.
    23 
    24     public static function init() // Initialize WP Snippets.
     22    /**
     23     * Flag for initializer.
     24     *
     25     * @var boolean Initialized?
     26     */
     27    public static $initialized = FALSE;
     28
     29    /**
     30     * Roles to receive all caps.
     31     *
     32     * @var array Array of WP roles.
     33     */
     34    public static $roles_all_caps = array();
     35
     36    /**
     37     * Roles to receive edit caps.
     38     *
     39     * @var array Array of WP roles.
     40     */
     41    public static $roles_edit_caps = array();
     42
     43    /**
     44     * Flag used by shortcode parser.
     45     *
     46     * @var boolean Currently parsing?
     47     */
     48    public static $is_parsing = FALSE;
     49
     50    /**
     51     * Plugin activation handler.
     52     */
     53    public static function activate()
     54    {
     55        self::initialize();
     56        self::caps('activate');
     57        flush_rewrite_rules();
     58    }
     59
     60    /**
     61     * Plugin deactivation handler.
     62     */
     63    public static function deactivate()
     64    {
     65        self::initialize();
     66        self::caps('deactivate');
     67        flush_rewrite_rules();
     68    }
     69
     70    /**
     71     * Plugin initializer.
     72     */
     73    public static function initialize()
     74    {
     75        if(self::$initialized)
     76            return; // Already did this.
     77
     78        load_plugin_textdomain('wp-snippets');
     79
     80        $GLOBALS['snippet_post'] = NULL; // Initialize.
     81
     82        if(!defined('WP_SNIPPET_ROLES_ALL_CAPS'))
     83            define('WP_SNIPPET_ROLES_ALL_CAPS', 'administrator');
     84
     85        if(!defined('WP_SNIPPET_ROLES_EDIT_CAPS'))
     86            define('WP_SNIPPET_ROLES_EDIT_CAPS', 'administrator,editor,author');
     87
     88        if(WP_SNIPPET_ROLES_ALL_CAPS) // Certain roles should receive all snippet-related caps?
     89            self::$roles_all_caps = preg_split('/[\s;,]+/', WP_SNIPPET_ROLES_ALL_CAPS, NULL, PREG_SPLIT_NO_EMPTY);
     90        self::$roles_all_caps = apply_filters('wp_snippet_roles_all_caps', self::$roles_all_caps);
     91
     92        if(WP_SNIPPET_ROLES_EDIT_CAPS) // Certain roles should receive edit-related caps for snippets?
     93            self::$roles_edit_caps = preg_split('/[\s;,]+/', WP_SNIPPET_ROLES_EDIT_CAPS, NULL, PREG_SPLIT_NO_EMPTY);
     94        self::$roles_edit_caps = apply_filters('wp_snippet_roles_edit_caps', self::$roles_edit_caps);
     95
     96        self::register(); // Register snippet post type.
     97
     98        add_filter('widget_text', 'do_shortcode');
     99        add_shortcode('snippet', 'wp_snippets::shortcode');
     100        add_shortcode('snippet_template', 'wp_snippets::shortcode');
     101
     102        if(defined('RAWHTML_PLUGIN_FILE') && function_exists('rawhtml_get_settings_fields'))
     103            add_filter('get_post_metadata', 'wp_snippets::raw_html_settings', 10, 4);
     104
     105        self::$initialized = TRUE; // Initialized now.
     106    }
     107
     108    /**
     109     * Registers the snippet post type.
     110     */
     111    public static function register()
     112    {
     113        $post_type_args           = array
     114        (
     115            'public'       => current_user_can('edit_snippets'), // For previews.
     116            'show_ui'      => TRUE, 'exclude_from_search' => TRUE, 'show_in_nav_menus' => FALSE,
     117            'map_meta_cap' => TRUE, 'capability_type' => array('snippet', 'snippets'),
     118            'rewrite'      => array('slug' => 'snippet', 'with_front' => FALSE),
     119            'supports'     => array('title', 'editor', 'author', 'revisions'),
     120        );
     121        $post_type_args['labels'] = array
     122        (
     123            'name'               => __('Snippets', 'wp-snippets'),
     124            'singular_name'      => __('Snippet', 'wp-snippets'),
     125            'add_new'            => __('Add Snippet', 'wp-snippets'),
     126            'add_new_item'       => __('Add New Snippet', 'wp-snippets'),
     127            'edit_item'          => __('Edit Snippet', 'wp-snippets'),
     128            'new_item'           => __('New Snippet', 'wp-snippets'),
     129            'all_items'          => __('All Snippets', 'wp-snippets'),
     130            'view_item'          => __('View Snippet', 'wp-snippets'),
     131            'search_items'       => __('Search Snippets', 'wp-snippets'),
     132            'not_found'          => __('No Snippets found', 'wp-snippets'),
     133            'not_found_in_trash' => __('No Snippets found in Trash', 'wp-snippets'),
     134        );
     135        register_post_type('snippet', $post_type_args);
     136
     137        $taxonomy_args = array // Categories.
     138        (
     139                               'public'       => TRUE, 'show_admin_column' => TRUE,
     140                               'hierarchical' => TRUE, // This will use category labels.
     141                               'rewrite'      => array('slug' => 'snippet-category', 'with_front' => FALSE),
     142                               'capabilities' => array('assign_terms' => 'edit_snippets',
     143                                                       'edit_terms'   => 'edit_snippets',
     144                                                       'manage_terms' => 'edit_others_snippets',
     145                                                       'delete_terms' => 'delete_others_snippets'),
     146        );
     147        register_taxonomy('snippet_category', array('snippet'), $taxonomy_args);
     148    }
     149
     150    /**
     151     * Activates or deactivates snippet-related caps.
     152     *
     153     * @param string $action One of `activate` or `deactivate`.
     154     */
     155    public static function caps($action)
     156    {
     157        $all_caps = array // The ability to manage (all caps).
     158        (
     159                          'edit_snippets',
     160                          'edit_others_snippets',
     161                          'edit_published_snippets',
     162                          'edit_private_snippets',
     163
     164                          'publish_snippets',
     165
     166                          'delete_snippets',
     167                          'delete_private_snippets',
     168                          'delete_published_snippets',
     169                          'delete_others_snippets',
     170
     171                          'read_private_snippets',
     172        );
     173        if($action === 'deactivate') // All on deactivate.
     174            $_roles = array_keys($GLOBALS['wp_roles']->roles);
     175        else $_roles = self::$roles_all_caps;
     176
     177        foreach($_roles as $_role) if(is_object($_role = get_role($_role)))
     178            foreach($all_caps as $_cap) switch($action)
     179            {
     180                case 'activate':
     181                    $_role->add_cap($_cap);
     182                    break;
     183
     184                case 'deactivate':
     185                    $_role->remove_cap($_cap);
     186                    break;
     187            }
     188        unset($_roles, $_role, $_cap); // Housekeeping.
     189
     190        $edit_caps = array // The ability to edit/publish/delete.
     191        (
     192                           'edit_snippets',
     193                           'edit_published_snippets',
     194
     195                           'publish_snippets',
     196
     197                           'delete_snippets',
     198                           'delete_published_snippets',
     199        );
     200        if($action === 'deactivate') // All on deactivate.
     201            $_roles = array_keys($GLOBALS['wp_roles']->roles);
     202        else $_roles = self::$roles_edit_caps;
     203
     204        foreach($_roles as $_role) if(is_object($_role = get_role($_role)))
     205            foreach((($action === 'deactivate') ? $all_caps : $edit_caps) as $_cap) switch($action)
     206            {
     207                case 'activate':
     208                    $_role->add_cap($_cap);
     209                    break;
     210
     211                case 'deactivate':
     212                    $_role->remove_cap($_cap);
     213                    break;
     214            }
     215        unset($_roles, $_role, $_cap); // Housekeeping.
     216    }
     217
     218    /**
     219     * Filters Raw HTML plugin settings for a specific post.
     220     *
     221     * @param mixed   $what_wp_says Current post meta value.
     222     * @param integer $post_id Post ID.
     223     * @param string  $meta_key Post meta key.
     224     * @param boolean $single Return a single value?
     225     *
     226     * @return mixed Post meta value after having been filtered by this routine.
     227     */
     228    public static function raw_html_settings($what_wp_says, $post_id, $meta_key, $single)
     229    {
     230        if(function_exists('rawhtml_get_settings_fields'))
     231            if($meta_key === '_rawhtml_settings' && get_post_type($post_id) === 'snippet')
     232            {
     233                $settings = // Force all `disable_` flags off; Snippets must use `[raw][/raw]` tags.
     234                    implode(',', array_fill(0, count(rawhtml_get_settings_fields()), '0'));
     235
     236                return $single ? $settings : array($settings);
     237            }
     238        return $what_wp_says; // Default return value.
     239    }
     240
     241    /**
     242     * Shortcode (i.e. snippet) parser.
     243     *
     244     * @param array|null  $attr An array of shortcode attributes.
     245     * @param string|null $content Shortcode content; if applicable.
     246     * @param string|null $shortcode The name of the shortcode.
     247     *
     248     * @return string Shortcode (i.e. snippet) content.
     249     */
     250    public static function shortcode($attr = NULL, $content = NULL, $shortcode = NULL)
     251    {
     252        # Validation and DB query.
     253
     254        if(empty($attr['slug'])) return ''; // Nothing to do.
     255
     256        if(!is_array($posts = get_posts(array('name' => (string)$attr['slug'], 'post_type' => 'snippet', 'numberposts' => 1))))
     257            return ''; // The slug was not found; possibly a typo in this case.
     258
     259        if(empty($posts[0]) || empty($posts[0]->post_content))
     260            return ''; // No content; nothing to do.
     261
     262        # Snippet and its content.
     263
     264        $snippet         = $posts[0]; // Object reference.
     265        $snippet_content = $snippet->post_content;
     266
     267        # Parse replacement codes in snippet templates.
     268
     269        if($shortcode === 'snippet_template') // A snippet template?
    25270        {
    26             load_plugin_textdomain('wp-snippets');
    27 
    28             if(WP_SNIPPET_ROLES_ALL_CAPS) // Specific Roles?
    29                 wp_snippets::$roles_all_caps = // Convert these to an array.
    30                     preg_split('/[\s;,]+/', WP_SNIPPET_ROLES_ALL_CAPS, NULL, PREG_SPLIT_NO_EMPTY);
    31             wp_snippets::$roles_all_caps = apply_filters('wp_snippet_roles_all_caps', wp_snippets::$roles_all_caps);
    32 
    33             if(WP_SNIPPET_ROLES_EDIT_CAPS) // Specific Roles?
    34                 wp_snippets::$roles_edit_caps = // Convert these to an array.
    35                     preg_split('/[\s;,]+/', WP_SNIPPET_ROLES_EDIT_CAPS, NULL, PREG_SPLIT_NO_EMPTY);
    36             wp_snippets::$roles_edit_caps = apply_filters('wp_snippet_roles_edit_caps', wp_snippets::$roles_edit_caps);
    37 
    38             wp_snippets::register();
    39 
    40             add_filter('widget_text', 'do_shortcode');
    41             add_shortcode('snippet', 'wp_snippets::shortcode');
    42             add_shortcode('snippet_template', 'wp_snippets::shortcode');
    43 
    44             if(defined('RAWHTML_PLUGIN_FILE') && function_exists('rawhtml_get_settings_fields'))
    45                 add_filter('get_post_metadata', 'wp_snippets::raw_html_settings', 10, 4);
    46 
    47             $GLOBALS['snippet_post'] = NULL; // Initialize this reference.
     271            foreach($attr as $_key => $_value) // Fill replacement codes.
     272                $snippet_content = str_ireplace('%%'.$_key.'%%', (string)$_value, $snippet_content);
     273            unset($_key, $_value); // Housekeeping.
     274
     275            $snippet_content = preg_replace('/%%content%%/', (string)$content, $snippet_content);
     276            $snippet_content = preg_replace('/%%.+?%%/', '', $snippet_content);
    48277        }
    49 
    50     public static function register()
     278        # Setup post data; and detect top-level snippets here.
     279
     280        $is_top_level_snippet = FALSE; // Initialize.
     281        $wp_current_filter    = $wp_current_filter_temp = NULL;
     282
     283        if(!self::$is_parsing) // Not currently parsing?
    51284        {
    52             $post_type_args           = array
    53             (
    54                 'public'       => current_user_can('edit_snippets'), // For previews.
    55                 'show_ui'      => TRUE, 'exclude_from_search' => TRUE, 'show_in_nav_menus' => FALSE,
    56                 'map_meta_cap' => TRUE, 'capability_type' => array('snippet', 'snippets'),
    57                 'rewrite'      => array('slug' => 'snippet', 'with_front' => FALSE),
    58                 'supports'     => array('title', 'editor', 'author', 'revisions')
    59             );
    60             $post_type_args['labels'] = array
    61             (
    62                 'name'               => __('Snippets', 'wp-snippets'),
    63                 'singular_name'      => __('Snippet', 'wp-snippets'),
    64                 'add_new'            => __('Add Snippet', 'wp-snippets'),
    65                 'add_new_item'       => __('Add New Snippet', 'wp-snippets'),
    66                 'edit_item'          => __('Edit Snippet', 'wp-snippets'),
    67                 'new_item'           => __('New Snippet', 'wp-snippets'),
    68                 'all_items'          => __('All Snippets', 'wp-snippets'),
    69                 'view_item'          => __('View Snippet', 'wp-snippets'),
    70                 'search_items'       => __('Search Snippets', 'wp-snippets'),
    71                 'not_found'          => __('No Snippets found', 'wp-snippets'),
    72                 'not_found_in_trash' => __('No Snippets found in Trash', 'wp-snippets')
    73             );
    74             register_post_type('snippet', $post_type_args);
    75 
    76             $taxonomy_args = array // Categories.
    77             (
    78                 'public'       => TRUE, 'show_admin_column' => TRUE,
    79                 'hierarchical' => TRUE, // This will use category labels.
    80                 'rewrite'      => array('slug' => 'snippet-category', 'with_front' => FALSE),
    81                 'capabilities' => array('assign_terms' => 'edit_snippets',
    82                                         'edit_terms'   => 'edit_snippets',
    83                                         'manage_terms' => 'edit_others_snippets',
    84                                         'delete_terms' => 'delete_others_snippets')
    85             );
    86             register_taxonomy('snippet_category', array('snippet'), $taxonomy_args);
     285            self::$is_parsing        = TRUE;
     286            $is_top_level_snippet    = TRUE;
     287            $GLOBALS['snippet_post'] = $GLOBALS['post'];
     288
     289            $wp_current_filter      = &$GLOBALS['wp_filter'][current_filter()];
     290            $wp_current_filter_temp = $wp_current_filter; // Backup filter state.
    87291        }
    88 
    89     public static function caps($action)
     292        setup_postdata($GLOBALS['post'] = $snippet);
     293
     294        # Apply content/snippet filters.
     295
     296        $snippet_content = apply_filters('the_content', $snippet_content);
     297        $snippet_content = apply_filters('the_snippet_content', $snippet_content);
     298
     299        # Restore post data; if applicable.
     300
     301        if($is_top_level_snippet) // Only when done w/ top-level snippet.
    90302        {
    91             $all_caps = array // The ability to manage (all caps).
    92             (
    93                 'edit_snippets',
    94                 'edit_others_snippets',
    95                 'edit_published_snippets',
    96                 'edit_private_snippets',
    97 
    98                 'publish_snippets',
    99 
    100                 'delete_snippets',
    101                 'delete_private_snippets',
    102                 'delete_published_snippets',
    103                 'delete_others_snippets',
    104 
    105                 'read_private_snippets'
    106             );
    107             if($action === 'deactivate') // All on deactivate.
    108                 $_roles = array_keys($GLOBALS['wp_roles']->roles);
    109             else $_roles = wp_snippets::$roles_all_caps;
    110 
    111             foreach($_roles as $_role) if(is_object($_role = get_role($_role)))
    112                 foreach($all_caps as $_cap) switch($action)
    113                 {
    114                     case 'activate':
    115                             $_role->add_cap($_cap);
    116                             break;
    117 
    118                     case 'deactivate':
    119                             $_role->remove_cap($_cap);
    120                             break;
    121                 }
    122             unset($_roles, $_role, $_cap); // Housekeeping.
    123 
    124             $edit_caps = array // The ability to edit/publish/delete.
    125             (
    126                 'edit_snippets',
    127                 'edit_published_snippets',
    128 
    129                 'publish_snippets',
    130 
    131                 'delete_snippets',
    132                 'delete_published_snippets'
    133             );
    134             if($action === 'deactivate') // All on deactivate.
    135                 $_roles = array_keys($GLOBALS['wp_roles']->roles);
    136             else $_roles = wp_snippets::$roles_edit_caps;
    137 
    138             foreach($_roles as $_role) if(is_object($_role = get_role($_role)))
    139                 foreach((($action === 'deactivate') ? $all_caps : $edit_caps) as $_cap) switch($action)
    140                 {
    141                     case 'activate':
    142                             $_role->add_cap($_cap);
    143                             break;
    144 
    145                     case 'deactivate':
    146                             $_role->remove_cap($_cap);
    147                             break;
    148                 }
    149             unset($_roles, $_role, $_cap); // Housekeeping.
    150         }
    151 
    152     public static function raw_html_settings($what_wp_says, $post_id, $meta_key, $single)
    153         {
    154             if(function_exists('rawhtml_get_settings_fields'))
    155                 if($meta_key === '_rawhtml_settings' && get_post_type($post_id) === 'snippet')
    156                     {
    157                         $settings = // Force all `disable_` flags off; Snippets must use `[raw][/raw]` tags.
    158                             implode(',', array_fill(0, count(rawhtml_get_settings_fields()), '0'));
    159 
    160                         return ($single) ? $settings : array($settings);
    161                     }
    162             return $what_wp_says; // Default return value.
    163         }
    164 
    165     public static function shortcode($attr = NULL, $content = NULL, $shortcode = NULL)
    166         {
    167             if(empty($attr['slug'])) return ''; // Nothing to do in this case.
    168 
    169             if(!is_array($posts = get_posts(array('name' => (string)$attr['slug'], 'post_type' => 'snippet', 'numberposts' => 1))))
    170                 return ''; // The slug was not found; possibly a typo in this case.
    171 
    172             if(empty($posts[0]) || empty($posts[0]->post_content))
    173                 return ''; // No content; nothing to do.
    174 
    175             $snippet         = $posts[0]; // Object reference.
    176             $snippet_content = $snippet->post_content;
    177 
    178             if($shortcode === 'snippet_template') // Template?
    179                 {
    180                     foreach($attr as $_key => $_value)
    181                         $snippet_content = str_ireplace('%%'.$_key.'%%', (string)$_value, $snippet_content);
    182                     $snippet_content = preg_replace('/%%content%%/', (string)$content, $snippet_content);
    183                     $snippet_content = preg_replace('/%%.+?%%/', '', $snippet_content);
    184                     unset($_key, $_value); // Housekeeping.
    185                 }
    186             $GLOBALS['snippet_post'] = // Possible parent post reference.
    187                 $GLOBALS['post']; // This could be empty; Snippets can be in widgets too.
    188             setup_postdata($GLOBALS['post'] = $snippet); // For filters.
    189 
    190             $snippet_content = apply_filters('the_content', $snippet_content);
    191             $snippet_content = apply_filters('the_snippet_content', $snippet_content);
     303            $wp_current_filter = $wp_current_filter_temp; // Restore filter state.
    192304
    193305            $GLOBALS['post'] = $GLOBALS['snippet_post']; // Restore.
    194306            if(!empty($GLOBALS['post']->ID)) setup_postdata($GLOBALS['post']);
    195             $GLOBALS['snippet_post'] = NULL; // Nullify now.
    196 
    197             return $snippet_content;
     307
     308            $GLOBALS['snippet_post'] = NULL; // Nullify.
     309            self::$is_parsing        = FALSE; // Reset.
    198310        }
    199 
    200     public static function activate()
    201         {
    202             wp_snippets::init();
    203             wp_snippets::caps('activate');
    204             flush_rewrite_rules();
    205         }
    206 
    207     public static function deactivate()
    208         {
    209             wp_snippets::caps('deactivate');
    210             flush_rewrite_rules();
    211         }
     311        # Return snippet content now.
     312
     313        return $snippet_content;
     314    }
    212315}
    213316
    214 add_action('init', 'wp_snippets::init');
     317/*
     318 * Primary plugin hooks.
     319 */
     320add_action('init', 'wp_snippets::initialize');
    215321register_activation_hook(__FILE__, 'wp_snippets::activate');
    216322register_deactivation_hook(__FILE__, 'wp_snippets::deactivate');
Note: See TracChangeset for help on using the changeset viewer.