| @@ -1,74 +1,141 @@ | ||
| 1 | 1 | <?php // User Submitted Posts - Access Control |
| 2 | 2 | |
| 3 | 3 | /* |
| 4 | - Shortcode: require login based on capability | |
| 4 | + Shortcode: display content based on user capability | |
| 5 | 5 | Syntax: [usp_access cap="read" deny=""][/usp_access] |
| 6 | 6 | Can use {tag} to output <tag> |
| 7 | - See @ https://codex.wordpress.org/Roles_and_Capabilities#Capabilities | |
| 7 | + https://wordpress.org/documentation/article/roles-and-capabilities/ | |
| 8 | 8 | */ |
| 9 | + | |
| 9 | 10 | if (!function_exists('usp_access')) : |
| 11 | + | |
| 10 | 12 | function usp_access($attr, $content = null) { |
| 11 | - extract(shortcode_atts(array( | |
| 12 | - 'cap' => 'read', | |
| 13 | - 'deny' => '', | |
| 14 | - ), $attr)); | |
| 15 | 13 | |
| 14 | + extract(shortcode_atts(array('cap' => 'read', 'deny' => ''), $attr)); | |
| 15 | + | |
| 16 | + // deny message | |
| 17 | + | |
| 18 | + $deny = htmlspecialchars($deny, ENT_QUOTES); | |
| 19 | + | |
| 16 | 20 | $deny = str_replace("{", "<", $deny); |
| 17 | 21 | $deny = str_replace("}", ">", $deny); |
| 18 | 22 | |
| 23 | + $deny = wp_kses_post($deny); | |
| 24 | + | |
| 25 | + // content | |
| 26 | + | |
| 27 | + $content = htmlspecialchars($content, ENT_QUOTES); | |
| 28 | + | |
| 29 | + $content = str_replace("{", "<", $content); | |
| 30 | + $content = str_replace("}", ">", $content); | |
| 31 | + | |
| 32 | + $content = wp_kses_post($content); | |
| 33 | + | |
| 34 | + // | |
| 35 | + | |
| 19 | 36 | $caps = array_map('trim', explode(',', $cap)); |
| 20 | 37 | |
| 21 | 38 | foreach ($caps as $c) { |
| 39 | + | |
| 22 | 40 | if (current_user_can($c) && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 41 | + | |
| 23 | 42 | } |
| 43 | + | |
| 24 | 44 | return $deny; |
| 45 | + | |
| 25 | 46 | } |
| 47 | + | |
| 26 | 48 | add_shortcode('usp_access', 'usp_access'); |
| 49 | + | |
| 27 | 50 | endif; |
| 28 | 51 | |
| 29 | 52 | |
| 30 | 53 | |
| 31 | 54 | /* |
| 32 | - Shortcode: show content to visitors | |
| 55 | + Shortcode: display content to visitors (not logged in) | |
| 33 | 56 | Syntax: [usp_visitor deny=""][/usp_visitor] |
| 34 | 57 | Can use {tag} to output <tag> |
| 35 | 58 | */ |
| 59 | + | |
| 36 | 60 | if (!function_exists('usp_visitor')) : |
| 61 | + | |
| 37 | 62 | function usp_visitor($attr, $content = null) { |
| 38 | - extract(shortcode_atts(array( | |
| 39 | - 'deny' => '', | |
| 40 | - ), $attr)); | |
| 41 | 63 | |
| 64 | + extract(shortcode_atts(array('deny' => ''), $attr)); | |
| 65 | + | |
| 66 | + // deny message | |
| 67 | + | |
| 68 | + $deny = htmlspecialchars($deny, ENT_QUOTES); | |
| 69 | + | |
| 42 | 70 | $deny = str_replace("{", "<", $deny); |
| 43 | 71 | $deny = str_replace("}", ">", $deny); |
| 44 | 72 | |
| 73 | + $deny = wp_kses_post($deny); | |
| 74 | + | |
| 75 | + // content | |
| 76 | + | |
| 77 | + $content = htmlspecialchars($content, ENT_QUOTES); | |
| 78 | + | |
| 79 | + $content = str_replace("{", "<", $content); | |
| 80 | + $content = str_replace("}", ">", $content); | |
| 81 | + | |
| 82 | + $content = wp_kses_post($content); | |
| 83 | + | |
| 84 | + // | |
| 85 | + | |
| 45 | 86 | if ((!is_user_logged_in() && !is_null($content)) || is_feed()) return do_shortcode($content); |
| 87 | + | |
| 46 | 88 | return $deny; |
| 89 | + | |
| 47 | 90 | } |
| 91 | + | |
| 48 | 92 | add_shortcode('usp_visitor', 'usp_visitor'); |
| 93 | + | |
| 49 | 94 | endif; |
| 50 | 95 | |
| 51 | 96 | |
| 52 | 97 | |
| 53 | 98 | /* |
| 54 | - Shortcode: show content to members | |
| 99 | + Shortcode: display content to members (logged in) | |
| 55 | 100 | Syntax: [usp_member deny=""][/usp_member] |
| 56 | 101 | Can use {tag} to output <tag> |
| 57 | 102 | */ |
| 103 | + | |
| 58 | 104 | if (!function_exists('usp_member')) : |
| 105 | + | |
| 59 | 106 | function usp_member($attr, $content = null) { |
| 60 | - extract(shortcode_atts(array( | |
| 61 | - 'deny' => '', | |
| 62 | - ), $attr)); | |
| 63 | 107 | |
| 108 | + extract(shortcode_atts(array('deny' => ''), $attr)); | |
| 109 | + | |
| 110 | + // deny message | |
| 111 | + | |
| 112 | + $deny = htmlspecialchars($deny, ENT_QUOTES); | |
| 113 | + | |
| 64 | 114 | $deny = str_replace("{", "<", $deny); |
| 65 | 115 | $deny = str_replace("}", ">", $deny); |
| 66 | 116 | |
| 117 | + $deny = wp_kses_post($deny); | |
| 118 | + | |
| 119 | + // content | |
| 120 | + | |
| 121 | + $content = htmlspecialchars($content, ENT_QUOTES); | |
| 122 | + | |
| 123 | + $content = str_replace("{", "<", $content); | |
| 124 | + $content = str_replace("}", ">", $content); | |
| 125 | + | |
| 126 | + $content = wp_kses_post($content); | |
| 127 | + | |
| 128 | + // | |
| 129 | + | |
| 67 | 130 | if (is_user_logged_in() && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 131 | + | |
| 68 | 132 | return $deny; |
| 133 | + | |
| 69 | 134 | } |
| 135 | + | |
| 70 | 136 | add_shortcode('usp_member', 'usp_member'); |
| 137 | + | |
| 71 | 138 | endif; |
| 72 | 139 | |
| 73 | 140 | |
| 74 | 141 | |
| @@ -74,17 +141,27 @@ | ||
| 74 | 141 | |
| 75 | 142 | /* |
| 76 | 143 | Shortcode Empty Paragraph Fix |
| 77 | 144 | */ |
| 145 | + | |
| 78 | 146 | if (!function_exists('usp_shortcode_empty_p_fix')) : |
| 147 | + | |
| 79 | 148 | function usp_shortcode_empty_p_fix($content) { |
| 149 | + | |
| 80 | 150 | $array = array( |
| 151 | + | |
| 81 | 152 | '<p>[' => '[', |
| 82 | 153 | ']</p>' => ']', |
| 83 | 154 | ']<br />' => ']', |
| 84 | 155 | ']<br>' => ']' |
| 156 | + | |
| 85 | 157 | ); |
| 158 | + | |
| 86 | 159 | $content = strtr($content, $array); |
| 160 | + | |
| 87 | 161 | return $content; |
| 162 | + | |
| 88 | 163 | } |
| 164 | + | |
| 89 | 165 | add_filter('the_content', 'usp_shortcode_empty_p_fix'); |
| 90 | -endif; | |
| 166 | + | |
| 167 | +endif; | |