PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.0
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Controllers / Shortcodes.php

Shortcodes.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.0, at classes/Controllers/Shortcodes.php

125 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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<string,string|int|null> $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 // Convert classes to array.
76 $classes = ! empty( $atts['class'] ) ? explode( ' ', $atts['class'] ) : [];
77
78 $classes[] = 'content-control-container';
79 // @deprecated 2.0.0
80 $classes[] = 'jp-cc';
81
82 if ( user_meets_requirements( $atts['status'], $user_roles, $match_type ) ) {
83 $classes[] = 'content-control-accessible';
84 // @deprecated 2.0.0
85 $classes[] = 'jp-cc-accessible';
86 $container = '<div class="%1$s">%2$s</div>';
87 } else {
88 $classes[] = 'content-control-not-accessible';
89 // @deprecated 2.0.0
90 $classes[] = 'jp-cc-not-accessible';
91 $container = '<div class="%1$s">%3$s</div>';
92 }
93
94 $classes = implode( ' ', $classes );
95
96 return sprintf( $container, esc_attr( $classes ), do_shortcode( $content ), do_shortcode( $atts['message'] ) );
97 }
98
99 /**
100 * Takes set but empty attributes and sets them to true.
101 *
102 * These are typically valueless boolean attributes.
103 *
104 * @param array<string|int,string|int|null> $atts Array of shortcode attributes.
105 *
106 * @return (int|null|string|true)[]
107 *
108 * @psalm-return array<int|string, int|null|string|true>
109 */
110 public function normalize_empty_atts( $atts = [] ) {
111 if ( ! is_array( $atts ) || empty( $atts ) ) {
112 $atts = [];
113 }
114
115 foreach ( $atts as $attribute => $value ) {
116 if ( is_int( $attribute ) ) {
117 $atts[ strtolower( $value ) ] = true;
118 unset( $atts[ $attribute ] );
119 }
120 }
121
122 return $atts;
123 }
124 }
125