1 | <?php |
||
6 | class GVLogic_Shortcode { |
||
7 | |||
8 | private static $SUPPORTED_SCALAR_OPERATORS = array( 'is', 'isnot', 'contains', 'starts_with', 'ends_with' ); |
||
9 | |||
10 | private static $SUPPORTED_NUMERIC_OPERATORS = array( 'greater_than', 'less_than' ); |
||
11 | |||
12 | private static $SUPPORTED_ARRAY_OPERATORS = array( 'in', 'not_in', 'isnot', 'contains' ); |
||
13 | |||
14 | private static $SUPPORTED_CUSTOM_OPERATORS = array( 'equals', 'greater_than_or_is', 'greater_than_or_equals', 'less_than_or_is', 'less_than_or_equals', 'not_contains' ); |
||
15 | |||
16 | /** |
||
17 | * Attributes passed to the shortcode |
||
18 | * @var array |
||
19 | */ |
||
20 | var $passed_atts; |
||
21 | |||
22 | /** |
||
23 | * Content inside the shortcode, displayed if matched |
||
24 | * @var string |
||
25 | */ |
||
26 | var $passed_content; |
||
27 | |||
28 | /** |
||
29 | * Parsed attributes |
||
30 | * @var array |
||
31 | */ |
||
32 | var $atts = array(); |
||
33 | |||
34 | /** |
||
35 | * Parsed content, shown if matched |
||
36 | * @var string |
||
37 | */ |
||
38 | var $content = ''; |
||
39 | |||
40 | /** |
||
41 | * Content shown if not matched |
||
42 | * This is set by having `[else]` inside the $content block |
||
43 | * @var string |
||
44 | */ |
||
45 | var $else_content = ''; |
||
46 | |||
47 | /** |
||
48 | * The current shortcode name being processed |
||
49 | * @var string |
||
50 | */ |
||
51 | var $shortcode = 'gvlogic'; |
||
52 | |||
53 | /** |
||
54 | * The left side of the comparison |
||
55 | * @var string |
||
56 | */ |
||
57 | var $if = ''; |
||
58 | |||
59 | /** |
||
60 | * The right side of the comparison |
||
61 | * @var string |
||
62 | */ |
||
63 | var $comparison = ''; |
||
64 | |||
65 | /** |
||
66 | * The comparison operator |
||
67 | * @var string |
||
68 | */ |
||
69 | var $operation = 'is'; |
||
70 | |||
71 | /** |
||
72 | * Does the comparison pass? |
||
73 | * @var bool |
||
74 | */ |
||
75 | var $is_match = false; |
||
76 | |||
77 | /** |
||
78 | * @var GVLogic_Shortcode |
||
79 | */ |
||
80 | private static $instance; |
||
81 | |||
82 | /** |
||
83 | * Instantiate! |
||
84 | * @return GVLogic_Shortcode |
||
85 | */ |
||
86 | public static function get_instance() { |
||
94 | |||
95 | /** |
||
96 | * Add the WordPress hooks |
||
97 | * @return void |
||
98 | */ |
||
99 | private function __construct() { |
||
102 | |||
103 | /** |
||
104 | * Register the shortcode |
||
105 | * @return void |
||
106 | */ |
||
107 | private function add_hooks() { |
||
111 | |||
112 | /** |
||
113 | * Get array of supported operators |
||
114 | * @param bool $with_values |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | 2 | private function get_operators( $with_values = false ) { |
|
132 | |||
133 | /** |
||
134 | * Set the operation for the shortcode. |
||
135 | * @param string $operation |
||
136 | * |
||
137 | * @return bool True: it's an allowed operation type and was added. False: invalid operation type |
||
138 | */ |
||
139 | 2 | private function set_operation( $operation = '' ) { |
|
155 | |||
156 | /** |
||
157 | * Set the operation and comparison for the shortcode |
||
158 | * |
||
159 | * Loop through each attribute passed to the shortcode and see if it's a valid operator. If so, set it. |
||
160 | * Example: [gvlogic if="{example}" greater_than="5"] |
||
161 | * `greater_than` will be set as the operator |
||
162 | * `5` will be set as the comparison value |
||
163 | * |
||
164 | * @return bool True: we've got an operation and comparison value; False: no, we don't |
||
165 | */ |
||
166 | 2 | private function setup_operation_and_comparison() { |
|
180 | |||
181 | /** |
||
182 | * @param array $atts User defined attributes in shortcode tag. |
||
183 | * @param null $content |
||
184 | * @param string $shortcode_tag |
||
185 | * |
||
186 | * @return string|null |
||
187 | */ |
||
188 | 3 | public function shortcode( $atts = array(), $content = NULL, $shortcode_tag = '' ) { |
|
237 | |||
238 | /** |
||
239 | * Does the if and the comparison match? |
||
240 | * @uses GVCommon::matches_operation |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | 2 | private function set_is_match() { |
|
247 | |||
248 | /** |
||
249 | * Get the output for the shortcode, based on whether there's a matched value |
||
250 | * |
||
251 | * @return string HTML/Text output of the shortcode |
||
252 | */ |
||
253 | 2 | private function get_output() { |
|
275 | |||
276 | /** |
||
277 | * Check for `[else]` tag inside the shortcode content. If exists, set the else_content variable. |
||
278 | * If not, use the `else` attribute passed by the shortcode, if exists. |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | 3 | private function set_content_and_else_content() { |
|
303 | |||
304 | /** |
||
305 | * Handle additional conditional logic inside the [else] pseudo-shortcode |
||
306 | * |
||
307 | * @since 1.21.2 |
||
308 | * |
||
309 | * @param string $before_else Shortcode content before the [else] tag (if it exists) |
||
310 | * |
||
311 | * @return bool|string False: No [else if] statements found. Otherwise, return the matched content. |
||
312 | */ |
||
313 | 3 | private function process_elseif( $before_else ) { |
|
355 | |||
356 | /** |
||
357 | * Process the attributes passed to the shortcode. Make sure they're valid |
||
358 | * @return void |
||
359 | */ |
||
360 | 2 | private function parse_atts() { |
|
389 | } |
||
390 | |||
392 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.