PluginProbe
Redirection / 4.3
Redirection v4.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / url-flags.php

url-flags.php in Redirection 4.3, at models/url-flags.php

139 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Represent URL source flags
5 */
6 class Red_Source_Flags {
7 const QUERY_IGNORE = 'ignore';
8 const QUERY_EXACT = 'exact';
9 const QUERY_PASS = 'pass';
10
11 const FLAG_QUERY = 'flag_query';
12 const FLAG_CASE = 'flag_case';
13 const FLAG_TRAILING = 'flag_trailing';
14 const FLAG_REGEX = 'flag_regex';
15
16 private $flag_case = false;
17 private $flag_trailing = false;
18 private $flag_regex = false;
19 private $flag_query = self::QUERY_EXACT;
20 private $values_set = [];
21
22 public function __construct( $json = null ) {
23 if ( $json ) {
24 $this->set_flags( $json );
25 }
26 }
27
28 private function get_allowed_query() {
29 return [
30 self::QUERY_IGNORE,
31 self::QUERY_EXACT,
32 self::QUERY_PASS,
33 ];
34 }
35
36 /**
37 * Parse flag data
38 *
39 * @param array $json Flag data
40 */
41 public function set_flags( array $json ) {
42 if ( isset( $json[ self::FLAG_QUERY ] ) && in_array( $json[ self::FLAG_QUERY ], $this->get_allowed_query(), true ) ) {
43 $this->flag_query = $json[ self::FLAG_QUERY ];
44 }
45
46 if ( isset( $json[ self::FLAG_CASE ] ) && is_bool( $json[ self::FLAG_CASE ] ) ) {
47 $this->flag_case = $json[ self::FLAG_CASE ] ? true : false;
48 }
49
50 if ( isset( $json[ self::FLAG_TRAILING ] ) && is_bool( $json[ self::FLAG_TRAILING ] ) ) {
51 $this->flag_trailing = $json[ self::FLAG_TRAILING ] ? true : false;
52 }
53
54 if ( isset( $json[ self::FLAG_REGEX ] ) && is_bool( $json[ self::FLAG_REGEX ] ) ) {
55 $this->flag_regex = $json[ self::FLAG_REGEX ] ? true : false;
56
57 if ( $this->flag_regex ) {
58 // Regex auto-disables other things
59 $this->flag_query = self::QUERY_EXACT;
60 }
61 }
62
63 // Keep track of what values have been set, so we know what to override with defaults later
64 $this->values_set = array_intersect( array_keys( $json ), array_keys( $this->get_json() ) );
65 }
66
67 public function is_ignore_trailing() {
68 return $this->flag_trailing;
69 }
70
71 public function is_ignore_case() {
72 return $this->flag_case;
73 }
74
75 public function is_regex() {
76 return $this->flag_regex;
77 }
78
79 public function is_query_exact() {
80 return $this->flag_query === self::QUERY_EXACT;
81 }
82
83 public function is_query_ignore() {
84 return $this->flag_query === self::QUERY_IGNORE;
85 }
86
87 public function is_query_pass() {
88 return $this->flag_query === self::QUERY_PASS;
89 }
90
91 public function get_json() {
92 return [
93 self::FLAG_QUERY => $this->flag_query,
94 self::FLAG_CASE => $this->is_ignore_case(),
95 self::FLAG_TRAILING => $this->is_ignore_trailing(),
96 self::FLAG_REGEX => $this->is_regex(),
97 ];
98 }
99
100 /**
101 * Return flag data, with defaults removed from the data
102 */
103 public function get_json_without_defaults( $defaults ) {
104 $json = $this->get_json();
105
106 if ( count( $defaults ) > 0 ) {
107 foreach ( $json as $key => $value ) {
108 if ( isset( $defaults[ $key ] ) && $value === $defaults[ $key ] ) {
109 unset( $json[ $key ] );
110 }
111 }
112 }
113
114 return $json;
115 }
116
117 /**
118 * Return flag data, with defaults filling in any gaps not set
119 */
120 public function get_json_with_defaults() {
121 $settings = red_get_options();
122 $json = $this->get_json();
123 $defaults = [
124 self::FLAG_QUERY => $settings[ self::FLAG_QUERY ],
125 self::FLAG_CASE => $settings[ self::FLAG_CASE ],
126 self::FLAG_TRAILING => $settings[ self::FLAG_TRAILING ],
127 self::FLAG_REGEX => $settings[ self::FLAG_REGEX ],
128 ];
129
130 foreach ( $this->values_set as $key ) {
131 if ( ! isset( $json[ $key ] ) ) {
132 $json[ $key ] = $defaults[ $key ];
133 }
134 }
135
136 return $json;
137 }
138 }
139