| 1 |
<?php // User Submitted Posts - Access Control |
| 2 |
|
| 3 |
/* |
| 4 |
Shortcode: display content based on user capability |
| 5 |
Syntax: [usp_access cap="read" deny=""][/usp_access] |
| 6 |
Can use {tag} to output <tag> |
| 7 |
https://wordpress.org/documentation/article/roles-and-capabilities/ |
| 8 |
*/ |
| 9 |
|
| 10 |
if (!function_exists('usp_access')) : |
| 11 |
|
| 12 |
function usp_access($attr, $content = null) { |
| 13 |
|
| 14 |
extract(shortcode_atts(array('cap' => 'read', 'deny' => ''), $attr)); |
| 15 |
|
| 16 |
// deny message |
| 17 |
|
| 18 |
$deny = htmlspecialchars($deny, ENT_QUOTES); |
| 19 |
|
| 20 |
$deny = str_replace("{", "<", $deny); |
| 21 |
$deny = str_replace("}", ">", $deny); |
| 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 |
|
| 36 |
$caps = array_map('trim', explode(',', $cap)); |
| 37 |
|
| 38 |
foreach ($caps as $c) { |
| 39 |
|
| 40 |
if (current_user_can($c) && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
return $deny; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
add_shortcode('usp_access', 'usp_access'); |
| 49 |
|
| 50 |
endif; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
/* |
| 55 |
Shortcode: display content to visitors (not logged in) |
| 56 |
Syntax: [usp_visitor deny=""][/usp_visitor] |
| 57 |
Can use {tag} to output <tag> |
| 58 |
*/ |
| 59 |
|
| 60 |
if (!function_exists('usp_visitor')) : |
| 61 |
|
| 62 |
function usp_visitor($attr, $content = null) { |
| 63 |
|
| 64 |
extract(shortcode_atts(array('deny' => ''), $attr)); |
| 65 |
|
| 66 |
// deny message |
| 67 |
|
| 68 |
$deny = htmlspecialchars($deny, ENT_QUOTES); |
| 69 |
|
| 70 |
$deny = str_replace("{", "<", $deny); |
| 71 |
$deny = str_replace("}", ">", $deny); |
| 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 |
|
| 86 |
if ((!is_user_logged_in() && !is_null($content)) || is_feed()) return do_shortcode($content); |
| 87 |
|
| 88 |
return $deny; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
add_shortcode('usp_visitor', 'usp_visitor'); |
| 93 |
|
| 94 |
endif; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
/* |
| 99 |
Shortcode: display content to members (logged in) |
| 100 |
Syntax: [usp_member deny=""][/usp_member] |
| 101 |
Can use {tag} to output <tag> |
| 102 |
*/ |
| 103 |
|
| 104 |
if (!function_exists('usp_member')) : |
| 105 |
|
| 106 |
function usp_member($attr, $content = null) { |
| 107 |
|
| 108 |
extract(shortcode_atts(array('deny' => ''), $attr)); |
| 109 |
|
| 110 |
// deny message |
| 111 |
|
| 112 |
$deny = htmlspecialchars($deny, ENT_QUOTES); |
| 113 |
|
| 114 |
$deny = str_replace("{", "<", $deny); |
| 115 |
$deny = str_replace("}", ">", $deny); |
| 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 |
|
| 130 |
if (is_user_logged_in() && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 131 |
|
| 132 |
return $deny; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
add_shortcode('usp_member', 'usp_member'); |
| 137 |
|
| 138 |
endif; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
/* |
| 143 |
Shortcode Empty Paragraph Fix |
| 144 |
*/ |
| 145 |
|
| 146 |
if (!function_exists('usp_shortcode_empty_p_fix')) : |
| 147 |
|
| 148 |
function usp_shortcode_empty_p_fix($content) { |
| 149 |
|
| 150 |
$array = array( |
| 151 |
|
| 152 |
'<p>[' => '[', |
| 153 |
']</p>' => ']', |
| 154 |
']<br />' => ']', |
| 155 |
']<br>' => ']' |
| 156 |
|
| 157 |
); |
| 158 |
|
| 159 |
$content = strtr($content, $array); |
| 160 |
|
| 161 |
return $content; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
add_filter('the_content', 'usp_shortcode_empty_p_fix'); |
| 166 |
|
| 167 |
endif; |