-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathif-elseif-else-shortcode.php
More file actions
131 lines (120 loc) · 3.33 KB
/
if-elseif-else-shortcode.php
File metadata and controls
131 lines (120 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Plugin Name: if-elseif-else Shortcode
* Plugin URI: https://www.nathankowald.com/blog/2019/06/wordpress-shortcode-if-elseif-else-statements
* Description: Use if-elseif-else conditions in your editor with a shortcode.
* Author: Nathan Kowald
* Author URI: https://www.nathankowald.com
* Text Domain: if-elseif-else-shortcode
* Domain Path: /languages
* Version: 0.2.0
*
* @package If_Elseif_Else_Shortcode
*/
/**
* Inspired by: https://level7systems.co.uk/wordpress-shortcodes-else-statement
*/
// Register shortcode.
add_shortcode( 'if', 'iee_if_elseif_else_statement' );
/**
* @param $atts
* @param $content
*
* @return string
*/
function iee_if_elseif_else_statement( $atts, $content ) {
if ( empty( $atts ) ) {
return '';
}
$pattern_else = '[else]';
$pattern_elseif = "/\[elseif\s([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*)\s?([^\[\]]*)?\]/";
$callable = array_shift( $atts );
$content_if = $content;
$content_else = '';
$content_elseif = '';
$elseif = false;
$elseif_true_index = 0;
if ( ! iee_is_valid_callable( $callable ) ) {
return __( 'If shortcode error: [if] argument must be callable', 'if-elseif-else-shortcode' );
}
$if = (boolean) call_user_func_array( $callable, $atts );
// If condition is false: check elseif condition(s).
if ( ! $if &&
preg_match_all(
$pattern_elseif,
$content,
$matches,
PREG_SET_ORDER
) > 0
) {
foreach ( $matches as $match ) {
$callable = array_values( array_slice( $match, - 2 ) )[0];
if ( ! iee_is_valid_callable( $callable ) ) {
return __( 'If shortcode error: [elseif] argument must be callable', 'if-elseif-else-shortcode' );
}
$atts = array_values( array_slice( $match, - 1 ) )[0];
$callable_params = empty( $atts ) ? [] : explode( ' ', $atts );
$elseif = (boolean) call_user_func_array( $callable, $callable_params );
if ( $elseif ) {
// elseif condition is true: no need to check further.
break;
}
$elseif_true_index ++;
}
}
if ( strpos( $content, $pattern_else ) !== false ) {
list( $content_if, $content_else ) = explode( $pattern_else, $content, 2 );
}
$contents = preg_split( $pattern_elseif, $content_if );
if ( $contents !== false ) {
$content_if = array_shift( $contents );
}
if ( $elseif ) {
$content_elseif = $contents[ $elseif_true_index ];
}
return do_shortcode( $if ? $content_if : ( $elseif ? $content_elseif : $content_else ) );
}
/**
* Checks to see if the arguments passed to [if] and [elseif] are callable.
*
* @param $callable
*
* @return bool
*/
function iee_is_valid_callable( $callable ) {
return is_callable( $callable ) && in_array( $callable, iee_get_allowed_callables() );
}
/**
* List of allowed, filterable callables.
*
* @return mixed|void
*/
function iee_get_allowed_callables() {
$whitelist = [
'comments_open',
'get_field',
'is_404',
'is_admin',
'is_archive',
'is_author',
'is_category',
'is_day',
'is_feed',
'is_front_page',
'is_home',
'is_month',
'is_page',
'is_search',
'is_single',
'is_singular',
'is_sticky',
'is_super_admin',
'is_tag',
'is_tax',
'is_time',
'is_user_logged_in',
'is_year',
'pings_open',
];
return apply_filters( 'if_elseif_else_shortcode_allowed_callables', $whitelist );
}