| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shortcode setup. |
| 4 |
* |
| 5 |
* @copyright (c) 2023, Code Atlantic LLC. |
| 6 |
* @package ContentControl |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Controllers; |
| 10 |
|
| 11 |
use ContentControl\Base\Controller; |
| 12 |
|
| 13 |
use function ContentControl\user_meets_requirements; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Shortcodes |
| 19 |
* |
| 20 |
* @package ContentControl |
| 21 |
*/ |
| 22 |
class Shortcodes extends Controller { |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize Widgets |
| 26 |
*/ |
| 27 |
public function init() { |
| 28 |
add_shortcode( 'content_control', [ $this, 'content_control' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Process the [content_control] shortcode. |
| 33 |
* |
| 34 |
* @param array $atts Array or shortcode attributes. |
| 35 |
* @param string $content Content inside shortcode. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function content_control( $atts, $content = '' ) { |
| 40 |
// Deprecated. |
| 41 |
$deprecated_atts = shortcode_atts( [ |
| 42 |
'logged_out' => null, // @deprecated 2.0. |
| 43 |
'roles' => null, // @deprecated 2.0. |
| 44 |
], $atts ); |
| 45 |
|
| 46 |
$atts = shortcode_atts( [ |
| 47 |
'status' => 'logged_in', // 'logged_in' or 'logged_out |
| 48 |
'allowed_roles' => null, |
| 49 |
'excluded_roles' => null, |
| 50 |
'class' => '', |
| 51 |
'message' => $this->container->get_option( 'defaultDenialMessage', '' ), |
| 52 |
], $this->normalize_empty_atts( $atts ), 'content_control' ); |
| 53 |
|
| 54 |
// Handle old args. |
| 55 |
if ( isset( $deprecated_atts['logged_out'] ) ) { |
| 56 |
$atts['status'] = (bool) $deprecated_atts['logged_out'] ? 'logged_out' : 'logged_in'; |
| 57 |
} |
| 58 |
|
| 59 |
if ( isset( $deprecated_atts['roles'] ) && ! empty( $deprecated_atts['roles'] ) ) { |
| 60 |
$atts['allowed_roles'] = $deprecated_atts['roles']; |
| 61 |
} |
| 62 |
|
| 63 |
$user_roles = []; |
| 64 |
$match_type = 'any'; |
| 65 |
|
| 66 |
// Normalize args. |
| 67 |
if ( ! empty( $atts['excluded_roles'] ) ) { |
| 68 |
$user_roles = $atts['excluded_roles']; |
| 69 |
$match_type = 'exclude'; |
| 70 |
} elseif ( ! empty( $atts['allowed_roles'] ) ) { |
| 71 |
$user_roles = $atts['allowed_roles']; |
| 72 |
$match_type = 'match'; |
| 73 |
} |
| 74 |
|
| 75 |
$classes = $atts['class']; |
| 76 |
|
| 77 |
if ( ! is_array( $classes ) ) { |
| 78 |
$classes = ! empty( $classes ) ? explode( ' ', $classes ) : []; |
| 79 |
} |
| 80 |
|
| 81 |
$classes[] = 'content-control-container'; |
| 82 |
// @deprecated 2.0.0 |
| 83 |
$classes[] = 'jp-cc'; |
| 84 |
|
| 85 |
if ( user_meets_requirements( $atts['status'], $user_roles, $match_type ) ) { |
| 86 |
$classes[] = 'content-control-accessible'; |
| 87 |
// @deprecated 2.0.0 |
| 88 |
$classes[] = 'jp-cc-accessible'; |
| 89 |
$container = '<div class="%1$s">%2$s</div>'; |
| 90 |
} else { |
| 91 |
$classes[] = 'content-control-not-accessible'; |
| 92 |
// @deprecated 2.0.0 |
| 93 |
$classes[] = 'jp-cc-not-accessible'; |
| 94 |
$container = '<div class="%1$s">%3$s</div>'; |
| 95 |
} |
| 96 |
|
| 97 |
$classes = implode( ' ', $classes ); |
| 98 |
|
| 99 |
return sprintf( $container, esc_attr( $classes ), do_shortcode( $content ), do_shortcode( $atts['message'] ) ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Takes set but empty attributes and sets them to true. |
| 104 |
* |
| 105 |
* These are typically valueless boolean attributes. |
| 106 |
* |
| 107 |
* @param array $atts Array of shortcode attributes. |
| 108 |
* |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public function normalize_empty_atts( $atts = [] ) { |
| 112 |
if ( ! is_array( $atts ) ) { |
| 113 |
if ( empty( $atts ) ) { |
| 114 |
$atts = []; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
foreach ( $atts as $attribute => $value ) { |
| 119 |
if ( is_int( $attribute ) ) { |
| 120 |
$atts[ strtolower( $value ) ] = true; |
| 121 |
unset( $atts[ $attribute ] ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $atts; |
| 126 |
} |
| 127 |
} |
| 128 |
|