Plugin Directory

Changeset 3401073


Ignore:
Timestamp:
11/22/2025 08:01:05 PM (3 weeks ago)
Author:
dmccan
Message:

Sanitized shortcode inputs on save. Allowed em tags. Tested for WordPress 6.9 compatibility.

Location:
yada-wiki
Files:
32 added
5 edited

Legend:

Unmodified
Added
Removed
  • yada-wiki/trunk/inc/functions-admin.php

    r2125863 r3401073  
    163163        wp_die();
    164164    }
    165  
     165   
     166    /******************************
     167    * Sanitize shortcode input on save
     168    *******************************/
     169    function yada_wiki_process_shortcodes_on_save($post_id, $post, $update) {
     170   
     171        // Only run on standard post saves, not autosave or revision
     172        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
     173        if ( wp_is_post_revision($post_id) ) return;
     174       
     175        // Do we need to filter the saving of posts and pages also?
     176        $options = get_option( 'yada_wiki_settings' );
     177        if ( isset($options['yada_wiki_checkbox_editor_buttons_setting']) ) {
     178            $allowShortcodeOnPostsAndPages = true;
     179        }
     180        else {
     181            $allowShortcodeOnPostsAndPages = false;
     182        }
     183       
     184        if ( $allowShortcodeOnPostsAndPages === true ) {
     185            if ( $post->post_type !== 'yada_wiki' && $post->post_type !== 'post'  && $post->post_type !== 'page' ) return;
     186        } else {
     187            if ( $post->post_type !== 'yada_wiki') return;
     188        }   
     189   
     190        // Prevent infinite loop
     191        remove_action('save_post', 'yada_wiki_process_shortcodes_on_save', 10);
     192   
     193        $content = $post->post_content;
     194        $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5);
     195        $regex = get_shortcode_regex();
     196        $has_changes = false;
     197        $allowed_html = array(
     198            'em' => array(), // Allow <em> with no attributes
     199        );                     
     200   
     201        if (preg_match_all('/' . $regex . '/s', $content, $matches, PREG_SET_ORDER)) {
     202            foreach ($matches as $match) {
     203                if (isset($match[2]) && $match[2] === 'yadawiki') {
     204                    if (isset($match[3])) {
     205                        $atts = shortcode_parse_atts($match[3]);
     206                        // Proceed to process $atts
     207                    } else {
     208                        $atts = array();
     209                        // Proceed, but there will be no matches
     210                    }
     211                   
     212                    // Sanitize attributes
     213                    $atts['link'] = isset($atts['link']) ? wp_kses($atts['link'], $allowed_html) : '';     
     214                    $atts['show']   = isset($atts['show']) ? wp_kses($atts['show'], $allowed_html) : '';
     215                    $atts['anchor'] = isset($atts['anchor']) ? sanitize_text_field($atts['anchor']) : '';
     216   
     217                    // Rebuild the sanitized shortcode, always with double quotes for safety
     218                    $sanitized = '[yadawiki';
     219                    foreach ($atts as $key => $value) {
     220                        if ($value !== '') {
     221                            $sanitized .= " {$key}=\"" . esc_attr($value) . "\"";
     222                        }
     223                    }
     224                    $sanitized .= ']';
     225   
     226                    // Replace the original shortcode with sanitized version
     227                    $content = str_replace($match[0], $sanitized, $content);
     228                    $has_changes = true;
     229                } elseif (isset($match[2]) && $match[2] === 'yadawikitoc') {
     230                    if (isset($match[3])) {
     231                        $atts = shortcode_parse_atts($match[3]);
     232                        // Proceed to process $atts
     233                    } else {
     234                        $atts = array();
     235                        // Proceed, but there will be no matches
     236                    }
     237   
     238                    // Sanitize attributes
     239                    $atts['show_toc'] = isset($atts['show_toc']) ? sanitize_text_field($atts['show_toc']) : '';
     240                    $atts['category']   = isset($atts['category']) ? sanitize_text_field($atts['category']) : '';
     241                    $atts['order'] = isset($atts['order']) ? sanitize_text_field($atts['order']) : '';
     242   
     243                    // Rebuild the sanitized shortcode, always with double quotes for safety
     244                    $sanitized = '[yadawikitoc';
     245                    foreach ($atts as $key => $value) {
     246                        if ($value !== '') {
     247                            $sanitized .= " {$key}=\"" . esc_attr($value) . "\"";
     248                        }
     249                    }
     250                    $sanitized .= ']';
     251   
     252                    // Replace the original shortcode with sanitized version
     253                    $content = str_replace($match[0], $sanitized, $content);
     254                    $has_changes = true;
     255                }  elseif (isset($match[2]) && $match[2] === 'yadawiki-index') {
     256                    if (isset($match[3])) {
     257                        $atts = shortcode_parse_atts($match[3]);
     258                        // Proceed to process $atts
     259                    } else {
     260                        $atts = array();
     261                        // Proceed, but there will be no matches
     262                    }
     263   
     264                    // Sanitize attributes
     265                    $atts['type'] = isset($atts['type']) ? sanitize_text_field($atts['type']) : '';
     266                    $atts['columns'] = isset($atts['columns']) ? sanitize_text_field($atts['columns']) : '';
     267   
     268                    // Rebuild the sanitized shortcode, always with double quotes for safety
     269                    $sanitized = '[yadawiki-index';
     270                    foreach ($atts as $key => $value) {
     271                        if ($value !== '') {
     272                            $sanitized .= " {$key}=\"" . esc_attr($value) . "\"";
     273                        }
     274                    }
     275                    $sanitized .= ']';
     276   
     277                    // Replace the original shortcode with sanitized version
     278                    $content = str_replace($match[0], $sanitized, $content);
     279                    $has_changes = true;
     280                }
     281            }
     282        }
     283   
     284        if ($has_changes) {
     285            wp_update_post([
     286                'ID' => $post_id,
     287                'post_content' => $content
     288            ]);
     289        }
     290   
     291        // Re-add the hook
     292        add_action('save_post', 'yada_wiki_process_shortcodes_on_save', 10, 3);
     293    }
     294
    166295    /********************************************************
    167296    * Funciton from Ohad Raz - https://en.bainternet.info/
  • yada-wiki/trunk/inc/functions-public.php

    r3049211 r3401073  
    1515        'anchor' => '',
    1616    ), $atts ) );
    17    
     17   
    1818    $link = sanitize_text_field($link);
    1919    $show = sanitize_text_field($show);
     
    4343    );
    4444    if($target) { $target=$target[0]; }
    45    
     45    // Search again in case the page title has em tags around it by removing them for the search
     46    if(!$target) {
     47        $target = get_posts(
     48            array(
     49                'post_type'              => 'yada_wiki',
     50                'title'                  => sanitize_text_field($wiki_page),
     51                'post_status'            => 'all',
     52                'numberposts'            => 1,
     53                'update_post_term_cache' => false,
     54                'update_post_meta_cache' => false,           
     55                'orderby'                => 'post_date ID',
     56                'order'                  => 'ASC',
     57            )
     58        );
     59        if($target) { $target=$target[0]; }
     60    }
     61
    4662    if($anchor_jump) {
    4763        $firstchar = substr($anchor_jump,0,1);
  • yada-wiki/trunk/inc/functions-widgets.php

    r2887834 r3401073  
    1717        if( $instance) {
    1818            $title      = esc_attr($instance['title']);
     19            $title      = sanitize_text_field($instance['title']);
    1920            $category   = $instance['category'];
    2021            $order      = $instance['order'];
     
    213214        if( $instance) {
    214215            $title      = esc_attr($instance['title']);
     216            $title      = sanitize_text_field($instance['title']);
    215217            $num_posts  = $instance['num_posts'];
    216218            $show_date  = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  • yada-wiki/trunk/readme.txt

    r3271525 r3401073  
    33Tags: wiki, shortcode, page links, faq, knowledge base
    44Requires at least: 4.1
    5 Tested up to: 6.8
    6 Stable tag: 3.5
     5Tested up to: 6.9
     6Stable tag: 3.6
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1111
    1212== Description ==
    13 Yada Wiki provides a wiki post type, custom tags and categories, an index, and a table of contents option.  The plugin allows you to link your wiki pages together using the wiki page titles. 
     13Yada Wiki provides a wiki post type, custom tags and categories, an index, and a table of contents option.  The plugin allows you to link your wiki pages together using the wiki page titles.
     14
     15Note: As of Yada Wiki 3.6, for current users who have been manually adding HTML tags or special characters to their shortcodes, for security reasons these must be filtered on save. An exception was added for the EM tag because I saw support tickets where users said they were using this tag.  If you have been manually editing the shortcodes then you may want to test before installing version 3.6. 
    1416
    1517There are two easy to use shortcode buttons available on the editor toolbar.  Rather than try to remember the shortcodes and their values, it is recommended that you use these buttons to generate the shortcodes for you.
     
    173175== Changelog ==
    174176
     177= 3.6 =
     178* Sanitized shortcode inputs on save. Allowed em tags.
     179* Tested for WordPress 6.9 compatibility.
     180
    175181= 3.5 =
    176182* Fixed shortcode index options which were not working correctly.
  • yada-wiki/trunk/yada-wiki.php

    r3049211 r3401073  
    44 * Plugin URI:  https://www.webtng.com/yada-wiki-documentation
    55 * Description: This plugin provides a simple wiki for your WordPress site.
    6  * Version:     3.5
     6 * Version:     3.6
    77 * Author:      David McCan
    88 * Author URI:  https://www.webtng.com
     
    2121 *
    2222 * @package   YadaWiki
    23  * @version   3.5
     23 * @version   3.6
    2424 * @author    David McCan <[email protected]>
    2525 * @copyright Copyright (c) 2015-2024, David McCan
     
    208208        add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 );
    209209        add_action( 'init', 'yadawiki_load_settings' );
     210        add_shortcode('yadawiki', 'yada_wiki_shortcode');
     211        add_shortcode('yadawikitoc', 'yada_wiki_toc_shortcode');
     212        add_shortcode('yadawiki-index', 'yada_wiki_index_shortcode');
    210213       
    211214        // public facing
    212215        if ( ! is_admin() ) {
    213216            add_action( 'wp_enqueue_scripts', 'yada_wiki_scripts' );
    214             add_shortcode('yadawiki', 'yada_wiki_shortcode');
    215             add_shortcode('yadawikitoc', 'yada_wiki_toc_shortcode');
    216             add_shortcode('yadawiki-index', 'yada_wiki_index_shortcode');
    217217        }
    218218       
     
    224224            add_action( 'admin_menu', 'yada_wiki_add_admin_menu' );
    225225            add_action( 'admin_init', 'yada_wiki_settings_init' );
     226            add_action('save_post', 'yada_wiki_process_shortcodes_on_save', 10, 3);
    226227           
    227228            // Handle Gutenberg
Note: See TracChangeset for help on using the changeset viewer.