PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Macro / WPDA_Macro.php

WPDA_Macro.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Macro/WPDA_Macro.php

137 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Macro {
4
5 class WPDA_Macro {
6
7 protected $raw_code = null;
8 protected $raw_array = array();
9 protected $has_macros = false;
10
11 public function __construct( $raw_code ) {
12 $this->raw_code = $raw_code;
13 if ( null !== $this->raw_code && '' !== str_replace( ' ', '', $this->raw_code ) ) {
14 $this->raw_array = explode( "\n", $this->raw_code ); // phpcs:ignore -- 8.1 proof
15 if ( is_array( $this->raw_array ) && count( $this->raw_array ) > 2 ) { // phpcs:ignore -- 8.1 proof
16 $this->has_macros = true;
17 }
18 }
19 }
20
21 public function exe_macro() {
22 if ( $this->has_macros ) {
23 $code = $this->macro_if( $this->raw_array );
24
25 return implode( ' ', $code );
26 } else {
27 return $this->raw_code;
28 }
29 }
30
31 protected function macro_if( $code_array ) {
32 if ( ! is_array( $code_array ) ) {
33 return $code_array;
34 }
35
36 $codeif = array();
37 $totalifs = 0;
38
39 for ( $line = 0; $line < count( $code_array ); $line++ ) { // phpcs:ignore -- 8.1 proof
40 $code = $code_array[ $line ];
41 if ( $this->is_macro( 'if', $code ) ) {
42 $totalifs++;
43
44 // Process macro if
45 $line_start = $line;
46 $line_else = -1;
47 $line_end = $line; // prevent PHP errors
48 $ifs_found = 1;
49 for ( $i = $line + 1; $i < count( $code_array ); $i++ ) { // phpcs:ignore -- 8.1 proof
50 if ( $this->is_macro( 'if', $code_array[ $i ] ) ) {
51 $ifs_found++;
52 }
53
54 if ( $this->is_macro( 'else', $code_array[ $i ] ) ) {
55 if ( 1 === $ifs_found ) {
56 $line_else = $i;
57 }
58 }
59
60 if ( $this->is_macro( 'endif', $code_array[ $i ] ) ) {
61 if ( 1 === $ifs_found ) {
62 $line_end = $i;
63 } else {
64 $ifs_found--;
65 }
66 }
67 }
68
69 if ( -1 === $line_else ) {
70 // Get code between if and end if
71 $code_slice = array_slice( $code_array, $line_start + 1, $line_end - $line_start - 1 ); // phpcs:ignore -- 8.1 proof
72 } else {
73 // Get code between if and else
74 $code_slice = array_slice( $code_array, $line_start + 1, $line_else - $line_start - 1 ); // phpcs:ignore -- 8.1 proof
75 // Get code between else and end if
76 $code_else = array_slice( $code_array, $line_else + 1, $line_end - $line_else - 1 ); // phpcs:ignore -- 8.1 proof
77 }
78
79 // Process nested ifs
80 $code_slice = $this->macro_if( $code_slice );
81
82 $condition = null;
83 foreach ( array( '==', '!=', '<', '>' ) as $c ) { // Handle each condition individually in macro_if_check
84 if ( $this->macro_if_has( $code, $c, $condition ) ) {
85 if ( $this->macro_if_check( $condition, $c ) ) {
86 $codeif = array_merge( $codeif, $code_slice ); // phpcs:ignore -- 8.1 proof
87 } else {
88 if ( $line_else > -1 ) {
89 $codeif = array_merge( $codeif, $code_else ); // phpcs:ignore -- 8.1 proof
90 }
91 }
92 $line = $line_end;
93 break;
94 }
95 }
96 } else {
97 $codeif = array_merge( $codeif, array( $code_array[ $line ] ) ); // phpcs:ignore -- 8.1 proof
98 }
99 }
100
101 if ( $totalifs === 0 ) {
102 return $code_array;
103 } else {
104 return $codeif;
105 }
106 }
107
108 protected function macro_if_has( $code, $if, &$condition ) {
109 $condition = explode( $if, html_entity_decode( substr( str_replace( ' ', '', (string ) $code ), 8 ) ) ); // phpcs:ignore -- 8.1 proof
110 return is_array( $condition ) && count( $condition ) === 2; // phpcs:ignore -- 8.1 proof
111 }
112
113 protected function macro_if_check( $condition, $if ) {
114 switch ( $if ) {
115 case '==';
116 return is_array( $condition ) && count( $condition ) === 2 && $condition[0] == $condition[1]; // phpcs:ignore -- 8.1 proof
117 break;
118 case '!=';
119 return is_array( $condition ) && count( $condition ) === 2 && $condition[0] != $condition[1]; // phpcs:ignore -- 8.1 proof
120 break;
121 case '<';
122 return is_array( $condition ) && count( $condition ) === 2 && $condition[0] < $condition[1]; // phpcs:ignore -- 8.1 proof
123 break;
124 case '>';
125 return is_array( $condition ) && count( $condition ) === 2 && $condition[0] > $condition[1]; // phpcs:ignore -- 8.1 proof
126 break;
127 }
128 }
129
130 protected function is_macro( $macro, $code ) {
131 return "#macro{$macro}" === substr( str_replace( ' ', '', $code ), 0, strlen( "#macro{$macro}" ) );
132 }
133
134 }
135
136 }
137