| 1 |
<?php // User Submitted Posts - Access Control |
| 2 |
|
| 3 |
/* |
| 4 |
Shortcode: require login based on capability |
| 5 |
Syntax: [usp_access cap="read" deny=""][/usp_access] |
| 6 |
Can use {tag} to output <tag> |
| 7 |
See @ https://codex.wordpress.org/Roles_and_Capabilities#Capabilities |
| 8 |
*/ |
| 9 |
if (!function_exists('usp_access')) : |
| 10 |
function usp_access($attr, $content = null) { |
| 11 |
extract(shortcode_atts(array( |
| 12 |
'cap' => 'read', |
| 13 |
'deny' => '', |
| 14 |
), $attr)); |
| 15 |
|
| 16 |
$deny = str_replace("{", "<", $deny); |
| 17 |
$deny = str_replace("}", ">", $deny); |
| 18 |
|
| 19 |
$caps = array_map('trim', explode(',', $cap)); |
| 20 |
|
| 21 |
foreach ($caps as $c) { |
| 22 |
if (current_user_can($c) && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 23 |
} |
| 24 |
return $deny; |
| 25 |
} |
| 26 |
add_shortcode('usp_access', 'usp_access'); |
| 27 |
endif; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/* |
| 32 |
Shortcode: show content to visitors |
| 33 |
Syntax: [usp_visitor deny=""][/usp_visitor] |
| 34 |
Can use {tag} to output <tag> |
| 35 |
*/ |
| 36 |
if (!function_exists('usp_visitor')) : |
| 37 |
function usp_visitor($attr, $content = null) { |
| 38 |
extract(shortcode_atts(array( |
| 39 |
'deny' => '', |
| 40 |
), $attr)); |
| 41 |
|
| 42 |
$deny = str_replace("{", "<", $deny); |
| 43 |
$deny = str_replace("}", ">", $deny); |
| 44 |
|
| 45 |
if ((!is_user_logged_in() && !is_null($content)) || is_feed()) return do_shortcode($content); |
| 46 |
return $deny; |
| 47 |
} |
| 48 |
add_shortcode('usp_visitor', 'usp_visitor'); |
| 49 |
endif; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
Shortcode: show content to members |
| 55 |
Syntax: [usp_member deny=""][/usp_member] |
| 56 |
Can use {tag} to output <tag> |
| 57 |
*/ |
| 58 |
if (!function_exists('usp_member')) : |
| 59 |
function usp_member($attr, $content = null) { |
| 60 |
extract(shortcode_atts(array( |
| 61 |
'deny' => '', |
| 62 |
), $attr)); |
| 63 |
|
| 64 |
$deny = str_replace("{", "<", $deny); |
| 65 |
$deny = str_replace("}", ">", $deny); |
| 66 |
|
| 67 |
if (is_user_logged_in() && !is_null($content) && !is_feed()) return do_shortcode($content); |
| 68 |
return $deny; |
| 69 |
} |
| 70 |
add_shortcode('usp_member', 'usp_member'); |
| 71 |
endif; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
/* |
| 76 |
Shortcode Empty Paragraph Fix |
| 77 |
*/ |
| 78 |
if (!function_exists('usp_shortcode_empty_p_fix')) : |
| 79 |
function usp_shortcode_empty_p_fix($content) { |
| 80 |
$array = array( |
| 81 |
'<p>[' => '[', |
| 82 |
']</p>' => ']', |
| 83 |
']<br />' => ']', |
| 84 |
']<br>' => ']' |
| 85 |
); |
| 86 |
$content = strtr($content, $array); |
| 87 |
return $content; |
| 88 |
} |
| 89 |
add_filter('the_content', 'usp_shortcode_empty_p_fix'); |
| 90 |
endif; |
| 91 |
|