@@ -19,164 +19,164 @@ |
||
19 | 19 | */ |
20 | 20 | class FileSubmission implements FileSubmissionInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string original name on the client machine |
|
24 | - */ |
|
25 | - protected $name; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string mime type |
|
29 | - */ |
|
30 | - protected $type; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string file extension |
|
34 | - */ |
|
35 | - protected $extension; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var int in bytes |
|
39 | - */ |
|
40 | - protected $size; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var string local filepath to the temporary file |
|
44 | - */ |
|
45 | - protected $tmp_file; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values |
|
49 | - * although those aren't expected. |
|
50 | - */ |
|
51 | - protected $error_code; |
|
52 | - |
|
53 | - /** |
|
54 | - * FileSubmission constructor. |
|
55 | - * @param $name |
|
56 | - * @param $tmp_file |
|
57 | - * @param $size |
|
58 | - * @param null $error_code |
|
59 | - * @throws InvalidArgumentException |
|
60 | - */ |
|
61 | - public function __construct($name, $tmp_file, $size, $error_code = null) |
|
62 | - { |
|
63 | - $this->name = basename($name); |
|
64 | - $scheme = parse_url($tmp_file, PHP_URL_SCHEME); |
|
65 | - if (in_array($scheme, ['http', 'https'])) { |
|
66 | - // Wait a minute- just local filepaths please, no URL schemes allowed! |
|
67 | - throw new InvalidArgumentException( |
|
68 | - sprintf( |
|
69 | - // @codingStandardsIgnoreStart |
|
70 | - esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'), |
|
71 | - // @codingStandardsIgnoreEnd |
|
72 | - $scheme, |
|
73 | - $tmp_file |
|
74 | - ) |
|
75 | - ); |
|
76 | - } |
|
77 | - $this->tmp_file = (string) $tmp_file; |
|
78 | - $this->size = (int) $size; |
|
79 | - $this->error_code = (int) $error_code; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getName() |
|
86 | - { |
|
87 | - return $this->name; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Gets the file's mime type |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function getType() |
|
95 | - { |
|
96 | - if (!$this->type) { |
|
97 | - $this->type = $this->determineType(); |
|
98 | - } |
|
99 | - return $this->type; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @since $VID:$ |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - protected function determineType() |
|
107 | - { |
|
108 | - if (!$this->getTmpFile()) { |
|
109 | - return ''; |
|
110 | - } |
|
111 | - $finfo = new finfo(FILEINFO_MIME_TYPE); |
|
112 | - return $finfo->file($this->getTmpFile()); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Gets the file's extension. |
|
117 | - * @since $VID:$ |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function getExtension() |
|
121 | - { |
|
122 | - if (!$this->extension) { |
|
123 | - $this->extension = $this->determineExtension(); |
|
124 | - } |
|
125 | - return $this->extension; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Determine's the file's extension given the temporary file. |
|
130 | - * @since $VID:$ |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - protected function determineExtension() |
|
134 | - { |
|
135 | - $position_of_period = strrpos($this->getName(), '.'); |
|
136 | - if ($position_of_period === false) { |
|
137 | - return ''; |
|
138 | - } |
|
139 | - return mb_substr( |
|
140 | - $this->getName(), |
|
141 | - $position_of_period + 1 |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Gets the size of the file |
|
147 | - * @return int |
|
148 | - */ |
|
149 | - public function getSize() |
|
150 | - { |
|
151 | - return $this->size; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Gets the path to the temporary file which was uploaded. |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - public function getTmpFile() |
|
159 | - { |
|
160 | - return $this->tmp_file; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @since $VID:$ |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - public function __toString() |
|
168 | - { |
|
169 | - return $this->getName(); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Gets the error code PHP reported for the file upload. |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public function getErrorCode() |
|
177 | - { |
|
178 | - return $this->error_code; |
|
179 | - } |
|
22 | + /** |
|
23 | + * @var string original name on the client machine |
|
24 | + */ |
|
25 | + protected $name; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string mime type |
|
29 | + */ |
|
30 | + protected $type; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string file extension |
|
34 | + */ |
|
35 | + protected $extension; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var int in bytes |
|
39 | + */ |
|
40 | + protected $size; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var string local filepath to the temporary file |
|
44 | + */ |
|
45 | + protected $tmp_file; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values |
|
49 | + * although those aren't expected. |
|
50 | + */ |
|
51 | + protected $error_code; |
|
52 | + |
|
53 | + /** |
|
54 | + * FileSubmission constructor. |
|
55 | + * @param $name |
|
56 | + * @param $tmp_file |
|
57 | + * @param $size |
|
58 | + * @param null $error_code |
|
59 | + * @throws InvalidArgumentException |
|
60 | + */ |
|
61 | + public function __construct($name, $tmp_file, $size, $error_code = null) |
|
62 | + { |
|
63 | + $this->name = basename($name); |
|
64 | + $scheme = parse_url($tmp_file, PHP_URL_SCHEME); |
|
65 | + if (in_array($scheme, ['http', 'https'])) { |
|
66 | + // Wait a minute- just local filepaths please, no URL schemes allowed! |
|
67 | + throw new InvalidArgumentException( |
|
68 | + sprintf( |
|
69 | + // @codingStandardsIgnoreStart |
|
70 | + esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'), |
|
71 | + // @codingStandardsIgnoreEnd |
|
72 | + $scheme, |
|
73 | + $tmp_file |
|
74 | + ) |
|
75 | + ); |
|
76 | + } |
|
77 | + $this->tmp_file = (string) $tmp_file; |
|
78 | + $this->size = (int) $size; |
|
79 | + $this->error_code = (int) $error_code; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getName() |
|
86 | + { |
|
87 | + return $this->name; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Gets the file's mime type |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function getType() |
|
95 | + { |
|
96 | + if (!$this->type) { |
|
97 | + $this->type = $this->determineType(); |
|
98 | + } |
|
99 | + return $this->type; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @since $VID:$ |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + protected function determineType() |
|
107 | + { |
|
108 | + if (!$this->getTmpFile()) { |
|
109 | + return ''; |
|
110 | + } |
|
111 | + $finfo = new finfo(FILEINFO_MIME_TYPE); |
|
112 | + return $finfo->file($this->getTmpFile()); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Gets the file's extension. |
|
117 | + * @since $VID:$ |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function getExtension() |
|
121 | + { |
|
122 | + if (!$this->extension) { |
|
123 | + $this->extension = $this->determineExtension(); |
|
124 | + } |
|
125 | + return $this->extension; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Determine's the file's extension given the temporary file. |
|
130 | + * @since $VID:$ |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + protected function determineExtension() |
|
134 | + { |
|
135 | + $position_of_period = strrpos($this->getName(), '.'); |
|
136 | + if ($position_of_period === false) { |
|
137 | + return ''; |
|
138 | + } |
|
139 | + return mb_substr( |
|
140 | + $this->getName(), |
|
141 | + $position_of_period + 1 |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Gets the size of the file |
|
147 | + * @return int |
|
148 | + */ |
|
149 | + public function getSize() |
|
150 | + { |
|
151 | + return $this->size; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Gets the path to the temporary file which was uploaded. |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + public function getTmpFile() |
|
159 | + { |
|
160 | + return $this->tmp_file; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @since $VID:$ |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + public function __toString() |
|
168 | + { |
|
169 | + return $this->getName(); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Gets the error code PHP reported for the file upload. |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public function getErrorCode() |
|
177 | + { |
|
178 | + return $this->error_code; |
|
179 | + } |
|
180 | 180 | } |
181 | 181 | // End of file FileSubmission.php |
182 | 182 | // Location: EventEspresso\core\services\request\files/FileSubmission.php |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | */ |
94 | 94 | public function getType() |
95 | 95 | { |
96 | - if (!$this->type) { |
|
96 | + if ( ! $this->type) { |
|
97 | 97 | $this->type = $this->determineType(); |
98 | 98 | } |
99 | 99 | return $this->type; |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | protected function determineType() |
107 | 107 | { |
108 | - if (!$this->getTmpFile()) { |
|
108 | + if ( ! $this->getTmpFile()) { |
|
109 | 109 | return ''; |
110 | 110 | } |
111 | 111 | $finfo = new finfo(FILEINFO_MIME_TYPE); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | */ |
120 | 120 | public function getExtension() |
121 | 121 | { |
122 | - if (!$this->extension) { |
|
122 | + if ( ! $this->extension) { |
|
123 | 123 | $this->extension = $this->determineExtension(); |
124 | 124 | } |
125 | 125 | return $this->extension; |
@@ -17,158 +17,158 @@ |
||
17 | 17 | class FqcnLocator extends Locator |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var array $FQCNs |
|
22 | - */ |
|
23 | - protected $FQCNs = array(); |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array $namespaces |
|
27 | - */ |
|
28 | - protected $namespaces = array(); |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @access protected |
|
33 | - * @param string $namespace |
|
34 | - * @param string $namespace_base_dir |
|
35 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
36 | - */ |
|
37 | - protected function setNamespace($namespace, $namespace_base_dir) |
|
38 | - { |
|
39 | - if (! is_string($namespace)) { |
|
40 | - throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
41 | - } |
|
42 | - if (! is_string($namespace_base_dir)) { |
|
43 | - throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
44 | - } |
|
45 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * @access public |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function getFQCNs() |
|
54 | - { |
|
55 | - return $this->FQCNs; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @access public |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function count() |
|
64 | - { |
|
65 | - return count($this->FQCNs); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * given a valid namespace, will find all files that match the provided mask |
|
71 | - * |
|
72 | - * @access public |
|
73 | - * @param string|array $namespaces |
|
74 | - * @return FilesystemIterator |
|
75 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
76 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
77 | - */ |
|
78 | - public function locate($namespaces) |
|
79 | - { |
|
80 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | - throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
82 | - } |
|
83 | - foreach ((array) $namespaces as $namespace) { |
|
84 | - foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
|
85 | - $this->FQCNs[ $key ] = $file; |
|
86 | - } |
|
87 | - } |
|
88 | - return $this->FQCNs; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
93 | - |
|
94 | - /** |
|
95 | - * given a partial namespace, will find all files in that folder |
|
96 | - * ** PLZ NOTE ** |
|
97 | - * This assumes that all files within the specified folder should be loaded |
|
98 | - * |
|
99 | - * @access protected |
|
100 | - * @param array $partial_namespace |
|
101 | - * @return FilesystemIterator |
|
102 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
103 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
104 | - */ |
|
105 | - protected function FindFQCNsByNamespace($partial_namespace) |
|
106 | - { |
|
107 | - $iterator = new FilesystemIterator( |
|
108 | - $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
109 | - ); |
|
110 | - foreach ($this->flags as $flag) { |
|
111 | - $iterator->setFlags($flag); |
|
112 | - } |
|
113 | - if (iterator_count($iterator) === 0) { |
|
114 | - return array(); |
|
115 | - } |
|
116 | - foreach ($iterator as $file) { |
|
117 | - $file = \EEH_File::standardise_directory_separators($file); |
|
118 | - foreach ($this->namespaces as $namespace => $base_dir) { |
|
119 | - $namespace .= Psr4Autoloader::NS; |
|
120 | - if (strpos($file, $base_dir) === 0) { |
|
121 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
122 | - array($base_dir, DS, '.php'), |
|
123 | - array($namespace, Psr4Autoloader::NS, ''), |
|
124 | - $file |
|
125 | - ); |
|
126 | - } |
|
127 | - } |
|
128 | - } |
|
129 | - return $this->FQCNs; |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * getDirectoryFromPartialNamespace |
|
135 | - * |
|
136 | - * @access protected |
|
137 | - * @param string $partial_namespace almost fully qualified class name ? |
|
138 | - * @return string |
|
139 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
140 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
141 | - */ |
|
142 | - protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
143 | - { |
|
144 | - if (empty($partial_namespace)) { |
|
145 | - throw new InvalidClassException($partial_namespace); |
|
146 | - } |
|
147 | - // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
148 | - $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
149 | - // breakup the incoming namespace into segments then loop thru them |
|
150 | - $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
151 | - $prefix = null; |
|
152 | - $namespace_segments_to_try = $namespace_segments; |
|
153 | - $removed_namespace_segments = []; |
|
154 | - while (! empty($namespace_segments_to_try)) { |
|
155 | - $namespace_to_try = implode(Psr4Autoloader::NS, $namespace_segments_to_try); |
|
156 | - // check if there's a base directory registered for that namespace |
|
157 | - $prefix = $psr4_loader->prefixes($namespace_to_try . Psr4Autoloader::NS); |
|
158 | - // nope? then the incoming namespace is invalid |
|
159 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
160 | - break; |
|
161 | - } |
|
162 | - array_unshift( |
|
163 | - $removed_namespace_segments, |
|
164 | - array_pop($namespace_segments_to_try) |
|
165 | - ); |
|
166 | - }// nope? then the incoming namespace is invalid |
|
167 | - if (empty($prefix) || empty($prefix[0])) { |
|
168 | - throw new InvalidClassException($partial_namespace); |
|
169 | - } |
|
170 | - $this->setNamespace($namespace_to_try, $prefix[0]); |
|
171 | - // but if it's good, add that base directory to the rest of the path, and return it |
|
172 | - return $prefix[0] . implode(DS, $removed_namespace_segments) . DS; |
|
173 | - } |
|
20 | + /** |
|
21 | + * @var array $FQCNs |
|
22 | + */ |
|
23 | + protected $FQCNs = array(); |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array $namespaces |
|
27 | + */ |
|
28 | + protected $namespaces = array(); |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @access protected |
|
33 | + * @param string $namespace |
|
34 | + * @param string $namespace_base_dir |
|
35 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
36 | + */ |
|
37 | + protected function setNamespace($namespace, $namespace_base_dir) |
|
38 | + { |
|
39 | + if (! is_string($namespace)) { |
|
40 | + throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
|
41 | + } |
|
42 | + if (! is_string($namespace_base_dir)) { |
|
43 | + throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
|
44 | + } |
|
45 | + $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * @access public |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function getFQCNs() |
|
54 | + { |
|
55 | + return $this->FQCNs; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @access public |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function count() |
|
64 | + { |
|
65 | + return count($this->FQCNs); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * given a valid namespace, will find all files that match the provided mask |
|
71 | + * |
|
72 | + * @access public |
|
73 | + * @param string|array $namespaces |
|
74 | + * @return FilesystemIterator |
|
75 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
76 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
77 | + */ |
|
78 | + public function locate($namespaces) |
|
79 | + { |
|
80 | + if (! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | + throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
|
82 | + } |
|
83 | + foreach ((array) $namespaces as $namespace) { |
|
84 | + foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
|
85 | + $this->FQCNs[ $key ] = $file; |
|
86 | + } |
|
87 | + } |
|
88 | + return $this->FQCNs; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
93 | + |
|
94 | + /** |
|
95 | + * given a partial namespace, will find all files in that folder |
|
96 | + * ** PLZ NOTE ** |
|
97 | + * This assumes that all files within the specified folder should be loaded |
|
98 | + * |
|
99 | + * @access protected |
|
100 | + * @param array $partial_namespace |
|
101 | + * @return FilesystemIterator |
|
102 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
103 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
104 | + */ |
|
105 | + protected function FindFQCNsByNamespace($partial_namespace) |
|
106 | + { |
|
107 | + $iterator = new FilesystemIterator( |
|
108 | + $this->getDirectoryFromPartialNamespace($partial_namespace) |
|
109 | + ); |
|
110 | + foreach ($this->flags as $flag) { |
|
111 | + $iterator->setFlags($flag); |
|
112 | + } |
|
113 | + if (iterator_count($iterator) === 0) { |
|
114 | + return array(); |
|
115 | + } |
|
116 | + foreach ($iterator as $file) { |
|
117 | + $file = \EEH_File::standardise_directory_separators($file); |
|
118 | + foreach ($this->namespaces as $namespace => $base_dir) { |
|
119 | + $namespace .= Psr4Autoloader::NS; |
|
120 | + if (strpos($file, $base_dir) === 0) { |
|
121 | + $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
122 | + array($base_dir, DS, '.php'), |
|
123 | + array($namespace, Psr4Autoloader::NS, ''), |
|
124 | + $file |
|
125 | + ); |
|
126 | + } |
|
127 | + } |
|
128 | + } |
|
129 | + return $this->FQCNs; |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * getDirectoryFromPartialNamespace |
|
135 | + * |
|
136 | + * @access protected |
|
137 | + * @param string $partial_namespace almost fully qualified class name ? |
|
138 | + * @return string |
|
139 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
140 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
141 | + */ |
|
142 | + protected function getDirectoryFromPartialNamespace($partial_namespace) |
|
143 | + { |
|
144 | + if (empty($partial_namespace)) { |
|
145 | + throw new InvalidClassException($partial_namespace); |
|
146 | + } |
|
147 | + // load our PSR-4 Autoloader so we can get the list of registered namespaces from it |
|
148 | + $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader(); |
|
149 | + // breakup the incoming namespace into segments then loop thru them |
|
150 | + $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS)); |
|
151 | + $prefix = null; |
|
152 | + $namespace_segments_to_try = $namespace_segments; |
|
153 | + $removed_namespace_segments = []; |
|
154 | + while (! empty($namespace_segments_to_try)) { |
|
155 | + $namespace_to_try = implode(Psr4Autoloader::NS, $namespace_segments_to_try); |
|
156 | + // check if there's a base directory registered for that namespace |
|
157 | + $prefix = $psr4_loader->prefixes($namespace_to_try . Psr4Autoloader::NS); |
|
158 | + // nope? then the incoming namespace is invalid |
|
159 | + if (! empty($prefix) && ! empty($prefix[0])) { |
|
160 | + break; |
|
161 | + } |
|
162 | + array_unshift( |
|
163 | + $removed_namespace_segments, |
|
164 | + array_pop($namespace_segments_to_try) |
|
165 | + ); |
|
166 | + }// nope? then the incoming namespace is invalid |
|
167 | + if (empty($prefix) || empty($prefix[0])) { |
|
168 | + throw new InvalidClassException($partial_namespace); |
|
169 | + } |
|
170 | + $this->setNamespace($namespace_to_try, $prefix[0]); |
|
171 | + // but if it's good, add that base directory to the rest of the path, and return it |
|
172 | + return $prefix[0] . implode(DS, $removed_namespace_segments) . DS; |
|
173 | + } |
|
174 | 174 | } |
@@ -36,13 +36,13 @@ discard block |
||
36 | 36 | */ |
37 | 37 | protected function setNamespace($namespace, $namespace_base_dir) |
38 | 38 | { |
39 | - if (! is_string($namespace)) { |
|
39 | + if ( ! is_string($namespace)) { |
|
40 | 40 | throw new InvalidDataTypeException('$namespace', $namespace, 'string'); |
41 | 41 | } |
42 | - if (! is_string($namespace_base_dir)) { |
|
42 | + if ( ! is_string($namespace_base_dir)) { |
|
43 | 43 | throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string'); |
44 | 44 | } |
45 | - $this->namespaces[ $namespace ] = $namespace_base_dir; |
|
45 | + $this->namespaces[$namespace] = $namespace_base_dir; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function locate($namespaces) |
79 | 79 | { |
80 | - if (! (is_string($namespaces) || is_array($namespaces))) { |
|
80 | + if ( ! (is_string($namespaces) || is_array($namespaces))) { |
|
81 | 81 | throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array'); |
82 | 82 | } |
83 | 83 | foreach ((array) $namespaces as $namespace) { |
84 | 84 | foreach ($this->FindFQCNsByNamespace($namespace) as $key => $file) { |
85 | - $this->FQCNs[ $key ] = $file; |
|
85 | + $this->FQCNs[$key] = $file; |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | return $this->FQCNs; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | foreach ($this->namespaces as $namespace => $base_dir) { |
119 | 119 | $namespace .= Psr4Autoloader::NS; |
120 | 120 | if (strpos($file, $base_dir) === 0) { |
121 | - $this->FQCNs[] = Psr4Autoloader::NS . str_replace( |
|
121 | + $this->FQCNs[] = Psr4Autoloader::NS.str_replace( |
|
122 | 122 | array($base_dir, DS, '.php'), |
123 | 123 | array($namespace, Psr4Autoloader::NS, ''), |
124 | 124 | $file |
@@ -151,12 +151,12 @@ discard block |
||
151 | 151 | $prefix = null; |
152 | 152 | $namespace_segments_to_try = $namespace_segments; |
153 | 153 | $removed_namespace_segments = []; |
154 | - while (! empty($namespace_segments_to_try)) { |
|
154 | + while ( ! empty($namespace_segments_to_try)) { |
|
155 | 155 | $namespace_to_try = implode(Psr4Autoloader::NS, $namespace_segments_to_try); |
156 | 156 | // check if there's a base directory registered for that namespace |
157 | - $prefix = $psr4_loader->prefixes($namespace_to_try . Psr4Autoloader::NS); |
|
157 | + $prefix = $psr4_loader->prefixes($namespace_to_try.Psr4Autoloader::NS); |
|
158 | 158 | // nope? then the incoming namespace is invalid |
159 | - if (! empty($prefix) && ! empty($prefix[0])) { |
|
159 | + if ( ! empty($prefix) && ! empty($prefix[0])) { |
|
160 | 160 | break; |
161 | 161 | } |
162 | 162 | array_unshift( |
@@ -169,6 +169,6 @@ discard block |
||
169 | 169 | } |
170 | 170 | $this->setNamespace($namespace_to_try, $prefix[0]); |
171 | 171 | // but if it's good, add that base directory to the rest of the path, and return it |
172 | - return $prefix[0] . implode(DS, $removed_namespace_segments) . DS; |
|
172 | + return $prefix[0].implode(DS, $removed_namespace_segments).DS; |
|
173 | 173 | } |
174 | 174 | } |
@@ -15,21 +15,21 @@ |
||
15 | 15 | */ |
16 | 16 | interface JsonSerializableAndUnserializable |
17 | 17 | { |
18 | - /** |
|
19 | - * Creates a simple PHP array or stdClass from this object's properties, which can be easily serialized using |
|
20 | - * wp_json_serialize(). |
|
21 | - * @since $VID:$ |
|
22 | - * @return mixed |
|
23 | - */ |
|
24 | - public function toJsonSerializableData(); |
|
18 | + /** |
|
19 | + * Creates a simple PHP array or stdClass from this object's properties, which can be easily serialized using |
|
20 | + * wp_json_serialize(). |
|
21 | + * @since $VID:$ |
|
22 | + * @return mixed |
|
23 | + */ |
|
24 | + public function toJsonSerializableData(); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Initializes this object from data |
|
28 | - * @since $VID:$ |
|
29 | - * @param mixed $data |
|
30 | - * @return boolean success |
|
31 | - */ |
|
32 | - public function fromJsonSerializedData($data); |
|
26 | + /** |
|
27 | + * Initializes this object from data |
|
28 | + * @since $VID:$ |
|
29 | + * @param mixed $data |
|
30 | + * @return boolean success |
|
31 | + */ |
|
32 | + public function fromJsonSerializedData($data); |
|
33 | 33 | } |
34 | 34 | // End of file JsonSerializableAndUnserializable.php |
35 | 35 | // Location: EventEspresso\core\services\json/JsonSerializableAndUnserializable.php |
@@ -29,641 +29,641 @@ |
||
29 | 29 | abstract class FormHandler implements FormHandlerInterface |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * will add opening and closing HTML form tags as well as a submit button |
|
34 | - */ |
|
35 | - const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
36 | - |
|
37 | - /** |
|
38 | - * will add opening and closing HTML form tags but NOT a submit button |
|
39 | - */ |
|
40 | - const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
41 | - |
|
42 | - /** |
|
43 | - * will NOT add opening and closing HTML form tags but will add a submit button |
|
44 | - */ |
|
45 | - const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
46 | - |
|
47 | - /** |
|
48 | - * will NOT add opening and closing HTML form tags NOR a submit button |
|
49 | - */ |
|
50 | - const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
51 | - |
|
52 | - /** |
|
53 | - * if set to false, then this form has no displayable content, |
|
54 | - * and will only be used for processing data sent passed via GET or POST |
|
55 | - * defaults to true ( ie: form has displayable content ) |
|
56 | - * |
|
57 | - * @var boolean $displayable |
|
58 | - */ |
|
59 | - private $displayable = true; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string $form_name |
|
63 | - */ |
|
64 | - private $form_name; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string $admin_name |
|
68 | - */ |
|
69 | - private $admin_name; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var string $slug |
|
73 | - */ |
|
74 | - private $slug; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var string $submit_btn_text |
|
78 | - */ |
|
79 | - private $submit_btn_text; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var string $form_action |
|
83 | - */ |
|
84 | - private $form_action; |
|
85 | - |
|
86 | - /** |
|
87 | - * form params in key value pairs |
|
88 | - * can be added to form action URL or as hidden inputs |
|
89 | - * |
|
90 | - * @var array $form_args |
|
91 | - */ |
|
92 | - private $form_args = array(); |
|
93 | - |
|
94 | - /** |
|
95 | - * value of one of the string constant above |
|
96 | - * |
|
97 | - * @var string $form_config |
|
98 | - */ |
|
99 | - private $form_config; |
|
100 | - |
|
101 | - /** |
|
102 | - * whether or not the form was determined to be invalid |
|
103 | - * |
|
104 | - * @var boolean $form_has_errors |
|
105 | - */ |
|
106 | - private $form_has_errors; |
|
107 | - |
|
108 | - /** |
|
109 | - * the absolute top level form section being used on the page |
|
110 | - * |
|
111 | - * @var EE_Form_Section_Proper $form |
|
112 | - */ |
|
113 | - private $form; |
|
114 | - |
|
115 | - /** |
|
116 | - * @var EE_Registry $registry |
|
117 | - */ |
|
118 | - protected $registry; |
|
119 | - |
|
120 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
121 | - /** |
|
122 | - * Form constructor. |
|
123 | - * |
|
124 | - * @param string $form_name |
|
125 | - * @param string $admin_name |
|
126 | - * @param string $slug |
|
127 | - * @param string $form_action |
|
128 | - * @param string $form_config |
|
129 | - * @param EE_Registry $registry |
|
130 | - * @throws InvalidDataTypeException |
|
131 | - * @throws DomainException |
|
132 | - * @throws InvalidArgumentException |
|
133 | - */ |
|
134 | - public function __construct( |
|
135 | - $form_name, |
|
136 | - $admin_name, |
|
137 | - $slug, |
|
138 | - $form_action = '', |
|
139 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
140 | - EE_Registry $registry |
|
141 | - ) { |
|
142 | - $this->setFormName($form_name); |
|
143 | - $this->setAdminName($admin_name); |
|
144 | - $this->setSlug($slug); |
|
145 | - $this->setFormAction($form_action); |
|
146 | - $this->setFormConfig($form_config); |
|
147 | - $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
148 | - $this->registry = $registry; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public static function getFormConfigConstants() |
|
156 | - { |
|
157 | - return array( |
|
158 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
159 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
160 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
161 | - FormHandler::DO_NOT_SETUP_FORM, |
|
162 | - ); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * @param bool $for_display |
|
168 | - * @return EE_Form_Section_Proper |
|
169 | - * @throws EE_Error |
|
170 | - * @throws LogicException |
|
171 | - */ |
|
172 | - public function form($for_display = false) |
|
173 | - { |
|
174 | - if (! $this->formIsValid()) { |
|
175 | - return null; |
|
176 | - } |
|
177 | - if ($for_display) { |
|
178 | - $form_config = $this->formConfig(); |
|
179 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
180 | - || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
181 | - ) { |
|
182 | - $this->appendSubmitButton(); |
|
183 | - $this->clearFormButtonFloats(); |
|
184 | - } |
|
185 | - } |
|
186 | - return $this->form; |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * @return boolean |
|
192 | - * @throws LogicException |
|
193 | - */ |
|
194 | - public function formIsValid() |
|
195 | - { |
|
196 | - if ($this->form instanceof EE_Form_Section_Proper) { |
|
197 | - return true; |
|
198 | - } |
|
199 | - $form = apply_filters( |
|
200 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
201 | - $this->generate(), |
|
202 | - $this |
|
203 | - ); |
|
204 | - if ($this->verifyForm($form)) { |
|
205 | - $this->setForm($form); |
|
206 | - } |
|
207 | - return true; |
|
208 | - } |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * @param EE_Form_Section_Proper|null $form |
|
213 | - * @return bool |
|
214 | - * @throws LogicException |
|
215 | - */ |
|
216 | - public function verifyForm(EE_Form_Section_Proper $form = null) |
|
217 | - { |
|
218 | - $form = $form !== null ? $form : $this->form; |
|
219 | - if ($form instanceof EE_Form_Section_Proper) { |
|
220 | - return true; |
|
221 | - } |
|
222 | - throw new LogicException( |
|
223 | - sprintf( |
|
224 | - esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
225 | - $this->form_name, |
|
226 | - var_export($form, true) |
|
227 | - ) |
|
228 | - ); |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * @param EE_Form_Section_Proper $form |
|
234 | - */ |
|
235 | - public function setForm(EE_Form_Section_Proper $form) |
|
236 | - { |
|
237 | - $this->form = $form; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * @return boolean |
|
243 | - */ |
|
244 | - public function displayable() |
|
245 | - { |
|
246 | - return $this->displayable; |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @param boolean $displayable |
|
252 | - */ |
|
253 | - public function setDisplayable($displayable = false) |
|
254 | - { |
|
255 | - $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * a public name for the form that can be displayed on the frontend of a site |
|
261 | - * |
|
262 | - * @return string |
|
263 | - */ |
|
264 | - public function formName() |
|
265 | - { |
|
266 | - return $this->form_name; |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $form_name |
|
272 | - * @throws InvalidDataTypeException |
|
273 | - */ |
|
274 | - public function setFormName($form_name) |
|
275 | - { |
|
276 | - if (! is_string($form_name)) { |
|
277 | - throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
278 | - } |
|
279 | - $this->form_name = $form_name; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * a public name for the form that can be displayed, but only in the admin |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function adminName() |
|
289 | - { |
|
290 | - return $this->admin_name; |
|
291 | - } |
|
292 | - |
|
293 | - |
|
294 | - /** |
|
295 | - * @param string $admin_name |
|
296 | - * @throws InvalidDataTypeException |
|
297 | - */ |
|
298 | - public function setAdminName($admin_name) |
|
299 | - { |
|
300 | - if (! is_string($admin_name)) { |
|
301 | - throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
302 | - } |
|
303 | - $this->admin_name = $admin_name; |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * a URL friendly string that can be used for identifying the form |
|
309 | - * |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - public function slug() |
|
313 | - { |
|
314 | - return $this->slug; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * @param string $slug |
|
320 | - * @throws InvalidDataTypeException |
|
321 | - */ |
|
322 | - public function setSlug($slug) |
|
323 | - { |
|
324 | - if (! is_string($slug)) { |
|
325 | - throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
326 | - } |
|
327 | - $this->slug = $slug; |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * @return string |
|
333 | - */ |
|
334 | - public function submitBtnText() |
|
335 | - { |
|
336 | - return $this->submit_btn_text; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * @param string $submit_btn_text |
|
342 | - * @throws InvalidDataTypeException |
|
343 | - * @throws InvalidArgumentException |
|
344 | - */ |
|
345 | - public function setSubmitBtnText($submit_btn_text) |
|
346 | - { |
|
347 | - if (! is_string($submit_btn_text)) { |
|
348 | - throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
349 | - } |
|
350 | - if (empty($submit_btn_text)) { |
|
351 | - throw new InvalidArgumentException( |
|
352 | - esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
353 | - ); |
|
354 | - } |
|
355 | - $this->submit_btn_text = $submit_btn_text; |
|
356 | - } |
|
357 | - |
|
358 | - |
|
359 | - /** |
|
360 | - * @return string |
|
361 | - */ |
|
362 | - public function formAction() |
|
363 | - { |
|
364 | - return ! empty($this->form_args) |
|
365 | - ? add_query_arg($this->form_args, $this->form_action) |
|
366 | - : $this->form_action; |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - /** |
|
371 | - * @param string $form_action |
|
372 | - * @throws InvalidDataTypeException |
|
373 | - */ |
|
374 | - public function setFormAction($form_action) |
|
375 | - { |
|
376 | - if (! is_string($form_action)) { |
|
377 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
378 | - } |
|
379 | - $this->form_action = $form_action; |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * @param array $form_args |
|
385 | - * @throws InvalidDataTypeException |
|
386 | - * @throws InvalidArgumentException |
|
387 | - */ |
|
388 | - public function addFormActionArgs($form_args = array()) |
|
389 | - { |
|
390 | - if (is_object($form_args)) { |
|
391 | - throw new InvalidDataTypeException( |
|
392 | - '$form_args', |
|
393 | - $form_args, |
|
394 | - 'anything other than an object was expected.' |
|
395 | - ); |
|
396 | - } |
|
397 | - if (empty($form_args)) { |
|
398 | - throw new InvalidArgumentException( |
|
399 | - esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
400 | - ); |
|
401 | - } |
|
402 | - $this->form_args = array_merge($this->form_args, $form_args); |
|
403 | - } |
|
404 | - |
|
405 | - |
|
406 | - /** |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - public function formConfig() |
|
410 | - { |
|
411 | - return $this->form_config; |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - /** |
|
416 | - * @param string $form_config |
|
417 | - * @throws DomainException |
|
418 | - */ |
|
419 | - public function setFormConfig($form_config) |
|
420 | - { |
|
421 | - if (! in_array( |
|
422 | - $form_config, |
|
423 | - array( |
|
424 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
425 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
426 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
427 | - FormHandler::DO_NOT_SETUP_FORM, |
|
428 | - ), |
|
429 | - true |
|
430 | - ) |
|
431 | - ) { |
|
432 | - throw new DomainException( |
|
433 | - sprintf( |
|
434 | - esc_html__( |
|
435 | - '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
436 | - 'event_espresso' |
|
437 | - ), |
|
438 | - $form_config |
|
439 | - ) |
|
440 | - ); |
|
441 | - } |
|
442 | - $this->form_config = $form_config; |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - /** |
|
447 | - * called after the form is instantiated |
|
448 | - * and used for performing any logic that needs to occur early |
|
449 | - * before any of the other methods are called. |
|
450 | - * returns true if everything is ok to proceed, |
|
451 | - * and false if no further form logic should be implemented |
|
452 | - * |
|
453 | - * @return boolean |
|
454 | - */ |
|
455 | - public function initialize() |
|
456 | - { |
|
457 | - $this->form_has_errors = EE_Error::has_error(true); |
|
458 | - return true; |
|
459 | - } |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * used for setting up css and js |
|
464 | - * |
|
465 | - * @return void |
|
466 | - * @throws LogicException |
|
467 | - * @throws EE_Error |
|
468 | - */ |
|
469 | - public function enqueueStylesAndScripts() |
|
470 | - { |
|
471 | - $this->form()->enqueue_js(); |
|
472 | - } |
|
473 | - |
|
474 | - |
|
475 | - /** |
|
476 | - * creates and returns the actual form |
|
477 | - * |
|
478 | - * @return EE_Form_Section_Proper |
|
479 | - */ |
|
480 | - abstract public function generate(); |
|
481 | - |
|
482 | - |
|
483 | - /** |
|
484 | - * creates and returns an EE_Submit_Input labeled "Submit" |
|
485 | - * |
|
486 | - * @param string $text |
|
487 | - * @return EE_Submit_Input |
|
488 | - */ |
|
489 | - public function generateSubmitButton($text = '') |
|
490 | - { |
|
491 | - $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
492 | - return new EE_Submit_Input( |
|
493 | - array( |
|
494 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
495 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
496 | - 'html_class' => 'ee-form-submit', |
|
497 | - 'html_label' => ' ', |
|
498 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
499 | - 'default' => $text, |
|
500 | - ) |
|
501 | - ); |
|
502 | - } |
|
503 | - |
|
504 | - |
|
505 | - /** |
|
506 | - * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
507 | - * |
|
508 | - * @param string $text |
|
509 | - * @return void |
|
510 | - * @throws EE_Error |
|
511 | - */ |
|
512 | - public function appendSubmitButton($text = '') |
|
513 | - { |
|
514 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
515 | - return; |
|
516 | - } |
|
517 | - $this->form->add_subsections( |
|
518 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
519 | - null, |
|
520 | - false |
|
521 | - ); |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * creates and returns an EE_Submit_Input labeled "Cancel" |
|
527 | - * |
|
528 | - * @param string $text |
|
529 | - * @return EE_Submit_Input |
|
530 | - */ |
|
531 | - public function generateCancelButton($text = '') |
|
532 | - { |
|
533 | - $cancel_button = new EE_Submit_Input( |
|
534 | - array( |
|
535 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
536 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
537 | - 'html_class' => 'ee-cancel-form', |
|
538 | - 'html_label' => ' ', |
|
539 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
540 | - 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
541 | - ) |
|
542 | - ); |
|
543 | - $cancel_button->set_button_css_attributes(false); |
|
544 | - return $cancel_button; |
|
545 | - } |
|
546 | - |
|
547 | - |
|
548 | - /** |
|
549 | - * appends a float clearing div onto end of form |
|
550 | - * |
|
551 | - * @return void |
|
552 | - * @throws EE_Error |
|
553 | - */ |
|
554 | - public function clearFormButtonFloats() |
|
555 | - { |
|
556 | - $this->form->add_subsections( |
|
557 | - array( |
|
558 | - 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
559 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
560 | - ), |
|
561 | - ), |
|
562 | - null, |
|
563 | - false |
|
564 | - ); |
|
565 | - } |
|
566 | - |
|
567 | - |
|
568 | - /** |
|
569 | - * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
570 | - * returns a string of HTML that can be directly echoed in a template |
|
571 | - * |
|
572 | - * @return string |
|
573 | - * @throws \InvalidArgumentException |
|
574 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
575 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
576 | - * @throws LogicException |
|
577 | - * @throws EE_Error |
|
578 | - */ |
|
579 | - public function display() |
|
580 | - { |
|
581 | - $form_html = apply_filters( |
|
582 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
583 | - '' |
|
584 | - ); |
|
585 | - $form_config = $this->formConfig(); |
|
586 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
587 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
588 | - ) { |
|
589 | - if ($this->requiresMultipartEnctype()) { |
|
590 | - $additional_props = 'enctype="multipart/form-data"'; |
|
591 | - } else { |
|
592 | - $additional_props = ''; |
|
593 | - } |
|
594 | - $form_html .= $this->form()->form_open( |
|
595 | - $this->formAction(), |
|
596 | - 'POST', |
|
597 | - $additional_props |
|
598 | - ); |
|
599 | - } |
|
600 | - $form_html .= $this->form(true)->get_html(); |
|
601 | - if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
602 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
603 | - ) { |
|
604 | - $form_html .= $this->form()->form_close(); |
|
605 | - } |
|
606 | - $form_html .= apply_filters( |
|
607 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
608 | - '' |
|
609 | - ); |
|
610 | - return $form_html; |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * Determines if this form needs " |
|
615 | - * @since $VID:$ |
|
616 | - * @return bool |
|
617 | - * @throws EE_Error |
|
618 | - */ |
|
619 | - public function requiresMultipartEnctype() |
|
620 | - { |
|
621 | - foreach ($this->form()->inputs_in_subsections() as $input) { |
|
622 | - if ($input instanceof EE_File_Input) { |
|
623 | - return true; |
|
624 | - } |
|
625 | - } |
|
626 | - return false; |
|
627 | - } |
|
628 | - |
|
629 | - |
|
630 | - /** |
|
631 | - * handles processing the form submission |
|
632 | - * returns true or false depending on whether the form was processed successfully or not |
|
633 | - * |
|
634 | - * @param array $submitted_form_data |
|
635 | - * @return array |
|
636 | - * @throws \InvalidArgumentException |
|
637 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
638 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
639 | - * @throws EE_Error |
|
640 | - * @throws LogicException |
|
641 | - * @throws InvalidFormSubmissionException |
|
642 | - */ |
|
643 | - public function process($submitted_form_data = array()) |
|
644 | - { |
|
645 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
646 | - throw new InvalidFormSubmissionException($this->form_name); |
|
647 | - } |
|
648 | - $this->form(true)->receive_form_submission($submitted_form_data); |
|
649 | - if (! $this->form()->is_valid()) { |
|
650 | - throw new InvalidFormSubmissionException( |
|
651 | - $this->form_name, |
|
652 | - sprintf( |
|
653 | - esc_html__( |
|
654 | - 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
655 | - 'event_espresso' |
|
656 | - ), |
|
657 | - $this->form_name, |
|
658 | - '<br />', |
|
659 | - implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
660 | - ) |
|
661 | - ); |
|
662 | - } |
|
663 | - return apply_filters( |
|
664 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
665 | - $this->form()->valid_data(), |
|
666 | - $this |
|
667 | - ); |
|
668 | - } |
|
32 | + /** |
|
33 | + * will add opening and closing HTML form tags as well as a submit button |
|
34 | + */ |
|
35 | + const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
36 | + |
|
37 | + /** |
|
38 | + * will add opening and closing HTML form tags but NOT a submit button |
|
39 | + */ |
|
40 | + const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
41 | + |
|
42 | + /** |
|
43 | + * will NOT add opening and closing HTML form tags but will add a submit button |
|
44 | + */ |
|
45 | + const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
46 | + |
|
47 | + /** |
|
48 | + * will NOT add opening and closing HTML form tags NOR a submit button |
|
49 | + */ |
|
50 | + const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
51 | + |
|
52 | + /** |
|
53 | + * if set to false, then this form has no displayable content, |
|
54 | + * and will only be used for processing data sent passed via GET or POST |
|
55 | + * defaults to true ( ie: form has displayable content ) |
|
56 | + * |
|
57 | + * @var boolean $displayable |
|
58 | + */ |
|
59 | + private $displayable = true; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string $form_name |
|
63 | + */ |
|
64 | + private $form_name; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string $admin_name |
|
68 | + */ |
|
69 | + private $admin_name; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var string $slug |
|
73 | + */ |
|
74 | + private $slug; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var string $submit_btn_text |
|
78 | + */ |
|
79 | + private $submit_btn_text; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var string $form_action |
|
83 | + */ |
|
84 | + private $form_action; |
|
85 | + |
|
86 | + /** |
|
87 | + * form params in key value pairs |
|
88 | + * can be added to form action URL or as hidden inputs |
|
89 | + * |
|
90 | + * @var array $form_args |
|
91 | + */ |
|
92 | + private $form_args = array(); |
|
93 | + |
|
94 | + /** |
|
95 | + * value of one of the string constant above |
|
96 | + * |
|
97 | + * @var string $form_config |
|
98 | + */ |
|
99 | + private $form_config; |
|
100 | + |
|
101 | + /** |
|
102 | + * whether or not the form was determined to be invalid |
|
103 | + * |
|
104 | + * @var boolean $form_has_errors |
|
105 | + */ |
|
106 | + private $form_has_errors; |
|
107 | + |
|
108 | + /** |
|
109 | + * the absolute top level form section being used on the page |
|
110 | + * |
|
111 | + * @var EE_Form_Section_Proper $form |
|
112 | + */ |
|
113 | + private $form; |
|
114 | + |
|
115 | + /** |
|
116 | + * @var EE_Registry $registry |
|
117 | + */ |
|
118 | + protected $registry; |
|
119 | + |
|
120 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
121 | + /** |
|
122 | + * Form constructor. |
|
123 | + * |
|
124 | + * @param string $form_name |
|
125 | + * @param string $admin_name |
|
126 | + * @param string $slug |
|
127 | + * @param string $form_action |
|
128 | + * @param string $form_config |
|
129 | + * @param EE_Registry $registry |
|
130 | + * @throws InvalidDataTypeException |
|
131 | + * @throws DomainException |
|
132 | + * @throws InvalidArgumentException |
|
133 | + */ |
|
134 | + public function __construct( |
|
135 | + $form_name, |
|
136 | + $admin_name, |
|
137 | + $slug, |
|
138 | + $form_action = '', |
|
139 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
140 | + EE_Registry $registry |
|
141 | + ) { |
|
142 | + $this->setFormName($form_name); |
|
143 | + $this->setAdminName($admin_name); |
|
144 | + $this->setSlug($slug); |
|
145 | + $this->setFormAction($form_action); |
|
146 | + $this->setFormConfig($form_config); |
|
147 | + $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
148 | + $this->registry = $registry; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public static function getFormConfigConstants() |
|
156 | + { |
|
157 | + return array( |
|
158 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
159 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
160 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
161 | + FormHandler::DO_NOT_SETUP_FORM, |
|
162 | + ); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * @param bool $for_display |
|
168 | + * @return EE_Form_Section_Proper |
|
169 | + * @throws EE_Error |
|
170 | + * @throws LogicException |
|
171 | + */ |
|
172 | + public function form($for_display = false) |
|
173 | + { |
|
174 | + if (! $this->formIsValid()) { |
|
175 | + return null; |
|
176 | + } |
|
177 | + if ($for_display) { |
|
178 | + $form_config = $this->formConfig(); |
|
179 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
180 | + || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
181 | + ) { |
|
182 | + $this->appendSubmitButton(); |
|
183 | + $this->clearFormButtonFloats(); |
|
184 | + } |
|
185 | + } |
|
186 | + return $this->form; |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * @return boolean |
|
192 | + * @throws LogicException |
|
193 | + */ |
|
194 | + public function formIsValid() |
|
195 | + { |
|
196 | + if ($this->form instanceof EE_Form_Section_Proper) { |
|
197 | + return true; |
|
198 | + } |
|
199 | + $form = apply_filters( |
|
200 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
201 | + $this->generate(), |
|
202 | + $this |
|
203 | + ); |
|
204 | + if ($this->verifyForm($form)) { |
|
205 | + $this->setForm($form); |
|
206 | + } |
|
207 | + return true; |
|
208 | + } |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * @param EE_Form_Section_Proper|null $form |
|
213 | + * @return bool |
|
214 | + * @throws LogicException |
|
215 | + */ |
|
216 | + public function verifyForm(EE_Form_Section_Proper $form = null) |
|
217 | + { |
|
218 | + $form = $form !== null ? $form : $this->form; |
|
219 | + if ($form instanceof EE_Form_Section_Proper) { |
|
220 | + return true; |
|
221 | + } |
|
222 | + throw new LogicException( |
|
223 | + sprintf( |
|
224 | + esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
225 | + $this->form_name, |
|
226 | + var_export($form, true) |
|
227 | + ) |
|
228 | + ); |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * @param EE_Form_Section_Proper $form |
|
234 | + */ |
|
235 | + public function setForm(EE_Form_Section_Proper $form) |
|
236 | + { |
|
237 | + $this->form = $form; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * @return boolean |
|
243 | + */ |
|
244 | + public function displayable() |
|
245 | + { |
|
246 | + return $this->displayable; |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @param boolean $displayable |
|
252 | + */ |
|
253 | + public function setDisplayable($displayable = false) |
|
254 | + { |
|
255 | + $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * a public name for the form that can be displayed on the frontend of a site |
|
261 | + * |
|
262 | + * @return string |
|
263 | + */ |
|
264 | + public function formName() |
|
265 | + { |
|
266 | + return $this->form_name; |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $form_name |
|
272 | + * @throws InvalidDataTypeException |
|
273 | + */ |
|
274 | + public function setFormName($form_name) |
|
275 | + { |
|
276 | + if (! is_string($form_name)) { |
|
277 | + throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
278 | + } |
|
279 | + $this->form_name = $form_name; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * a public name for the form that can be displayed, but only in the admin |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function adminName() |
|
289 | + { |
|
290 | + return $this->admin_name; |
|
291 | + } |
|
292 | + |
|
293 | + |
|
294 | + /** |
|
295 | + * @param string $admin_name |
|
296 | + * @throws InvalidDataTypeException |
|
297 | + */ |
|
298 | + public function setAdminName($admin_name) |
|
299 | + { |
|
300 | + if (! is_string($admin_name)) { |
|
301 | + throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
302 | + } |
|
303 | + $this->admin_name = $admin_name; |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * a URL friendly string that can be used for identifying the form |
|
309 | + * |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + public function slug() |
|
313 | + { |
|
314 | + return $this->slug; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * @param string $slug |
|
320 | + * @throws InvalidDataTypeException |
|
321 | + */ |
|
322 | + public function setSlug($slug) |
|
323 | + { |
|
324 | + if (! is_string($slug)) { |
|
325 | + throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
326 | + } |
|
327 | + $this->slug = $slug; |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * @return string |
|
333 | + */ |
|
334 | + public function submitBtnText() |
|
335 | + { |
|
336 | + return $this->submit_btn_text; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * @param string $submit_btn_text |
|
342 | + * @throws InvalidDataTypeException |
|
343 | + * @throws InvalidArgumentException |
|
344 | + */ |
|
345 | + public function setSubmitBtnText($submit_btn_text) |
|
346 | + { |
|
347 | + if (! is_string($submit_btn_text)) { |
|
348 | + throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
349 | + } |
|
350 | + if (empty($submit_btn_text)) { |
|
351 | + throw new InvalidArgumentException( |
|
352 | + esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
353 | + ); |
|
354 | + } |
|
355 | + $this->submit_btn_text = $submit_btn_text; |
|
356 | + } |
|
357 | + |
|
358 | + |
|
359 | + /** |
|
360 | + * @return string |
|
361 | + */ |
|
362 | + public function formAction() |
|
363 | + { |
|
364 | + return ! empty($this->form_args) |
|
365 | + ? add_query_arg($this->form_args, $this->form_action) |
|
366 | + : $this->form_action; |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + /** |
|
371 | + * @param string $form_action |
|
372 | + * @throws InvalidDataTypeException |
|
373 | + */ |
|
374 | + public function setFormAction($form_action) |
|
375 | + { |
|
376 | + if (! is_string($form_action)) { |
|
377 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
378 | + } |
|
379 | + $this->form_action = $form_action; |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * @param array $form_args |
|
385 | + * @throws InvalidDataTypeException |
|
386 | + * @throws InvalidArgumentException |
|
387 | + */ |
|
388 | + public function addFormActionArgs($form_args = array()) |
|
389 | + { |
|
390 | + if (is_object($form_args)) { |
|
391 | + throw new InvalidDataTypeException( |
|
392 | + '$form_args', |
|
393 | + $form_args, |
|
394 | + 'anything other than an object was expected.' |
|
395 | + ); |
|
396 | + } |
|
397 | + if (empty($form_args)) { |
|
398 | + throw new InvalidArgumentException( |
|
399 | + esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
400 | + ); |
|
401 | + } |
|
402 | + $this->form_args = array_merge($this->form_args, $form_args); |
|
403 | + } |
|
404 | + |
|
405 | + |
|
406 | + /** |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + public function formConfig() |
|
410 | + { |
|
411 | + return $this->form_config; |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + /** |
|
416 | + * @param string $form_config |
|
417 | + * @throws DomainException |
|
418 | + */ |
|
419 | + public function setFormConfig($form_config) |
|
420 | + { |
|
421 | + if (! in_array( |
|
422 | + $form_config, |
|
423 | + array( |
|
424 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
425 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
426 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
427 | + FormHandler::DO_NOT_SETUP_FORM, |
|
428 | + ), |
|
429 | + true |
|
430 | + ) |
|
431 | + ) { |
|
432 | + throw new DomainException( |
|
433 | + sprintf( |
|
434 | + esc_html__( |
|
435 | + '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
436 | + 'event_espresso' |
|
437 | + ), |
|
438 | + $form_config |
|
439 | + ) |
|
440 | + ); |
|
441 | + } |
|
442 | + $this->form_config = $form_config; |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + /** |
|
447 | + * called after the form is instantiated |
|
448 | + * and used for performing any logic that needs to occur early |
|
449 | + * before any of the other methods are called. |
|
450 | + * returns true if everything is ok to proceed, |
|
451 | + * and false if no further form logic should be implemented |
|
452 | + * |
|
453 | + * @return boolean |
|
454 | + */ |
|
455 | + public function initialize() |
|
456 | + { |
|
457 | + $this->form_has_errors = EE_Error::has_error(true); |
|
458 | + return true; |
|
459 | + } |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * used for setting up css and js |
|
464 | + * |
|
465 | + * @return void |
|
466 | + * @throws LogicException |
|
467 | + * @throws EE_Error |
|
468 | + */ |
|
469 | + public function enqueueStylesAndScripts() |
|
470 | + { |
|
471 | + $this->form()->enqueue_js(); |
|
472 | + } |
|
473 | + |
|
474 | + |
|
475 | + /** |
|
476 | + * creates and returns the actual form |
|
477 | + * |
|
478 | + * @return EE_Form_Section_Proper |
|
479 | + */ |
|
480 | + abstract public function generate(); |
|
481 | + |
|
482 | + |
|
483 | + /** |
|
484 | + * creates and returns an EE_Submit_Input labeled "Submit" |
|
485 | + * |
|
486 | + * @param string $text |
|
487 | + * @return EE_Submit_Input |
|
488 | + */ |
|
489 | + public function generateSubmitButton($text = '') |
|
490 | + { |
|
491 | + $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
492 | + return new EE_Submit_Input( |
|
493 | + array( |
|
494 | + 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
495 | + 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
496 | + 'html_class' => 'ee-form-submit', |
|
497 | + 'html_label' => ' ', |
|
498 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
499 | + 'default' => $text, |
|
500 | + ) |
|
501 | + ); |
|
502 | + } |
|
503 | + |
|
504 | + |
|
505 | + /** |
|
506 | + * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
507 | + * |
|
508 | + * @param string $text |
|
509 | + * @return void |
|
510 | + * @throws EE_Error |
|
511 | + */ |
|
512 | + public function appendSubmitButton($text = '') |
|
513 | + { |
|
514 | + if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
515 | + return; |
|
516 | + } |
|
517 | + $this->form->add_subsections( |
|
518 | + array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
519 | + null, |
|
520 | + false |
|
521 | + ); |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * creates and returns an EE_Submit_Input labeled "Cancel" |
|
527 | + * |
|
528 | + * @param string $text |
|
529 | + * @return EE_Submit_Input |
|
530 | + */ |
|
531 | + public function generateCancelButton($text = '') |
|
532 | + { |
|
533 | + $cancel_button = new EE_Submit_Input( |
|
534 | + array( |
|
535 | + 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
536 | + 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
537 | + 'html_class' => 'ee-cancel-form', |
|
538 | + 'html_label' => ' ', |
|
539 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
540 | + 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
541 | + ) |
|
542 | + ); |
|
543 | + $cancel_button->set_button_css_attributes(false); |
|
544 | + return $cancel_button; |
|
545 | + } |
|
546 | + |
|
547 | + |
|
548 | + /** |
|
549 | + * appends a float clearing div onto end of form |
|
550 | + * |
|
551 | + * @return void |
|
552 | + * @throws EE_Error |
|
553 | + */ |
|
554 | + public function clearFormButtonFloats() |
|
555 | + { |
|
556 | + $this->form->add_subsections( |
|
557 | + array( |
|
558 | + 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
559 | + EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
560 | + ), |
|
561 | + ), |
|
562 | + null, |
|
563 | + false |
|
564 | + ); |
|
565 | + } |
|
566 | + |
|
567 | + |
|
568 | + /** |
|
569 | + * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
570 | + * returns a string of HTML that can be directly echoed in a template |
|
571 | + * |
|
572 | + * @return string |
|
573 | + * @throws \InvalidArgumentException |
|
574 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
575 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
576 | + * @throws LogicException |
|
577 | + * @throws EE_Error |
|
578 | + */ |
|
579 | + public function display() |
|
580 | + { |
|
581 | + $form_html = apply_filters( |
|
582 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
583 | + '' |
|
584 | + ); |
|
585 | + $form_config = $this->formConfig(); |
|
586 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
587 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
588 | + ) { |
|
589 | + if ($this->requiresMultipartEnctype()) { |
|
590 | + $additional_props = 'enctype="multipart/form-data"'; |
|
591 | + } else { |
|
592 | + $additional_props = ''; |
|
593 | + } |
|
594 | + $form_html .= $this->form()->form_open( |
|
595 | + $this->formAction(), |
|
596 | + 'POST', |
|
597 | + $additional_props |
|
598 | + ); |
|
599 | + } |
|
600 | + $form_html .= $this->form(true)->get_html(); |
|
601 | + if ($form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
602 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
603 | + ) { |
|
604 | + $form_html .= $this->form()->form_close(); |
|
605 | + } |
|
606 | + $form_html .= apply_filters( |
|
607 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
608 | + '' |
|
609 | + ); |
|
610 | + return $form_html; |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * Determines if this form needs " |
|
615 | + * @since $VID:$ |
|
616 | + * @return bool |
|
617 | + * @throws EE_Error |
|
618 | + */ |
|
619 | + public function requiresMultipartEnctype() |
|
620 | + { |
|
621 | + foreach ($this->form()->inputs_in_subsections() as $input) { |
|
622 | + if ($input instanceof EE_File_Input) { |
|
623 | + return true; |
|
624 | + } |
|
625 | + } |
|
626 | + return false; |
|
627 | + } |
|
628 | + |
|
629 | + |
|
630 | + /** |
|
631 | + * handles processing the form submission |
|
632 | + * returns true or false depending on whether the form was processed successfully or not |
|
633 | + * |
|
634 | + * @param array $submitted_form_data |
|
635 | + * @return array |
|
636 | + * @throws \InvalidArgumentException |
|
637 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
638 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
639 | + * @throws EE_Error |
|
640 | + * @throws LogicException |
|
641 | + * @throws InvalidFormSubmissionException |
|
642 | + */ |
|
643 | + public function process($submitted_form_data = array()) |
|
644 | + { |
|
645 | + if (! $this->form()->was_submitted($submitted_form_data)) { |
|
646 | + throw new InvalidFormSubmissionException($this->form_name); |
|
647 | + } |
|
648 | + $this->form(true)->receive_form_submission($submitted_form_data); |
|
649 | + if (! $this->form()->is_valid()) { |
|
650 | + throw new InvalidFormSubmissionException( |
|
651 | + $this->form_name, |
|
652 | + sprintf( |
|
653 | + esc_html__( |
|
654 | + 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
655 | + 'event_espresso' |
|
656 | + ), |
|
657 | + $this->form_name, |
|
658 | + '<br />', |
|
659 | + implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
660 | + ) |
|
661 | + ); |
|
662 | + } |
|
663 | + return apply_filters( |
|
664 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
665 | + $this->form()->valid_data(), |
|
666 | + $this |
|
667 | + ); |
|
668 | + } |
|
669 | 669 | } |
@@ -17,108 +17,108 @@ |
||
17 | 17 | */ |
18 | 18 | class EE_File_Input extends EE_Form_Input_Base |
19 | 19 | { |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $allowed_file_extensions; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $allowed_file_extensions; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var |
|
27 | - */ |
|
28 | - protected $allowed_mime_types; |
|
25 | + /** |
|
26 | + * @var |
|
27 | + */ |
|
28 | + protected $allowed_mime_types; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param array $options |
|
32 | - * @throws InvalidArgumentException |
|
33 | - */ |
|
34 | - public function __construct($options = array()) |
|
35 | - { |
|
36 | - if (isset($options['allowed_file_extensions'])) { |
|
37 | - if (!is_array($options['allowed_file_extensions'])) { |
|
38 | - throw new InvalidArgumentException(esc_html__('A valid allowed_file_extensions array was not provided to EE_File_Input', 'event_espresso')); |
|
39 | - } |
|
40 | - $this->allowed_file_extensions = $options['allowed_file_extensions']; |
|
41 | - } else { |
|
42 | - $this->allowed_file_extensions = ['csv']; |
|
43 | - } |
|
44 | - if (isset($options['allowed_mime_types'])) { |
|
45 | - if (!is_array($options['allowed_mime_types'])) { |
|
46 | - throw new InvalidArgumentException(esc_html__('A valid allowed_mime_types array was not provided to EE_File_Input', 'event_espresso')); |
|
47 | - } |
|
48 | - $this->allowed_mime_types = $options['allowed_file_extensions']; |
|
49 | - } else { |
|
50 | - $this->allowed_mime_types = ['text/csv']; |
|
51 | - } |
|
30 | + /** |
|
31 | + * @param array $options |
|
32 | + * @throws InvalidArgumentException |
|
33 | + */ |
|
34 | + public function __construct($options = array()) |
|
35 | + { |
|
36 | + if (isset($options['allowed_file_extensions'])) { |
|
37 | + if (!is_array($options['allowed_file_extensions'])) { |
|
38 | + throw new InvalidArgumentException(esc_html__('A valid allowed_file_extensions array was not provided to EE_File_Input', 'event_espresso')); |
|
39 | + } |
|
40 | + $this->allowed_file_extensions = $options['allowed_file_extensions']; |
|
41 | + } else { |
|
42 | + $this->allowed_file_extensions = ['csv']; |
|
43 | + } |
|
44 | + if (isset($options['allowed_mime_types'])) { |
|
45 | + if (!is_array($options['allowed_mime_types'])) { |
|
46 | + throw new InvalidArgumentException(esc_html__('A valid allowed_mime_types array was not provided to EE_File_Input', 'event_espresso')); |
|
47 | + } |
|
48 | + $this->allowed_mime_types = $options['allowed_file_extensions']; |
|
49 | + } else { |
|
50 | + $this->allowed_mime_types = ['text/csv']; |
|
51 | + } |
|
52 | 52 | |
53 | - $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
54 | - $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
55 | - $this->add_validation_strategy( |
|
56 | - new EE_Text_Validation_Strategy( |
|
57 | - sprintf( |
|
58 | - // translators: %1$s is a list of allowed file extensions. |
|
59 | - esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
60 | - implode(', ', $this->allowed_file_extensions) |
|
61 | - ), |
|
62 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
63 | - ) |
|
64 | - ); |
|
65 | - parent::__construct($options); |
|
53 | + $this->_set_display_strategy(new EE_File_Input_Display_Strategy()); |
|
54 | + $this->_set_normalization_strategy(new EE_File_Normalization()); |
|
55 | + $this->add_validation_strategy( |
|
56 | + new EE_Text_Validation_Strategy( |
|
57 | + sprintf( |
|
58 | + // translators: %1$s is a list of allowed file extensions. |
|
59 | + esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
|
60 | + implode(', ', $this->allowed_file_extensions) |
|
61 | + ), |
|
62 | + '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
63 | + ) |
|
64 | + ); |
|
65 | + parent::__construct($options); |
|
66 | 66 | |
67 | 67 | // It would be great to add this HTML attribute, but jQuery validate chokes on it. |
68 | - $this->set_other_html_attributes( |
|
69 | - $this->other_html_attributes() |
|
70 | - . ' extension="' |
|
71 | - . implode( |
|
72 | - ',', |
|
73 | - // array_merge( |
|
74 | - // array_map( |
|
75 | - // function ($mime_type) { |
|
76 | - // if(strpos($mime_type, '/') === false) { |
|
77 | - // return $mime_type . '/*'; |
|
78 | - // } else { |
|
79 | - // return $mime_type; |
|
80 | - // } |
|
81 | - // |
|
82 | - // }, |
|
83 | - // $this->allowed_mime_types |
|
84 | - // ) |
|
85 | - array_map( |
|
86 | - function ($file_extension) { |
|
87 | - return $file_extension; |
|
88 | - }, |
|
89 | - $this->allowed_file_extensions |
|
90 | - ) |
|
91 | - // ) |
|
92 | - ) |
|
93 | - . '"' |
|
94 | - ); |
|
95 | - } |
|
68 | + $this->set_other_html_attributes( |
|
69 | + $this->other_html_attributes() |
|
70 | + . ' extension="' |
|
71 | + . implode( |
|
72 | + ',', |
|
73 | + // array_merge( |
|
74 | + // array_map( |
|
75 | + // function ($mime_type) { |
|
76 | + // if(strpos($mime_type, '/') === false) { |
|
77 | + // return $mime_type . '/*'; |
|
78 | + // } else { |
|
79 | + // return $mime_type; |
|
80 | + // } |
|
81 | + // |
|
82 | + // }, |
|
83 | + // $this->allowed_mime_types |
|
84 | + // ) |
|
85 | + array_map( |
|
86 | + function ($file_extension) { |
|
87 | + return $file_extension; |
|
88 | + }, |
|
89 | + $this->allowed_file_extensions |
|
90 | + ) |
|
91 | + // ) |
|
92 | + ) |
|
93 | + . '"' |
|
94 | + ); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file" |
|
99 | - * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with |
|
100 | - * "name", "size", "temp_file", etc. So that file's data is located at "my[name][hierarchical][form]", |
|
101 | - * "my[size][hierarchical][form]", "my[temp_name][hierarchical][form]", etc. It's really weird. |
|
102 | - * @since $VID:$ |
|
103 | - * @param array $req_data |
|
104 | - * @return array|mixed|NULL |
|
105 | - * @throws EE_Error |
|
106 | - */ |
|
107 | - public function find_form_data_for_this_section($req_data) |
|
108 | - { |
|
109 | - // ignore $req_data. Files are in the files data handler. |
|
110 | - $fileDataHandler = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\files\FilesDataHandler'); |
|
111 | - return $fileDataHandler->getFileObject($this->html_name()); |
|
112 | - } |
|
97 | + /** |
|
98 | + * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file" |
|
99 | + * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with |
|
100 | + * "name", "size", "temp_file", etc. So that file's data is located at "my[name][hierarchical][form]", |
|
101 | + * "my[size][hierarchical][form]", "my[temp_name][hierarchical][form]", etc. It's really weird. |
|
102 | + * @since $VID:$ |
|
103 | + * @param array $req_data |
|
104 | + * @return array|mixed|NULL |
|
105 | + * @throws EE_Error |
|
106 | + */ |
|
107 | + public function find_form_data_for_this_section($req_data) |
|
108 | + { |
|
109 | + // ignore $req_data. Files are in the files data handler. |
|
110 | + $fileDataHandler = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\files\FilesDataHandler'); |
|
111 | + return $fileDataHandler->getFileObject($this->html_name()); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Don't transform the file submission object into a string, thanks. |
|
116 | - * |
|
117 | - * @param string $value |
|
118 | - * @return null|string |
|
119 | - */ |
|
120 | - protected function _sanitize($value) |
|
121 | - { |
|
122 | - return $value; |
|
123 | - } |
|
114 | + /** |
|
115 | + * Don't transform the file submission object into a string, thanks. |
|
116 | + * |
|
117 | + * @param string $value |
|
118 | + * @return null|string |
|
119 | + */ |
|
120 | + protected function _sanitize($value) |
|
121 | + { |
|
122 | + return $value; |
|
123 | + } |
|
124 | 124 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | public function __construct($options = array()) |
35 | 35 | { |
36 | 36 | if (isset($options['allowed_file_extensions'])) { |
37 | - if (!is_array($options['allowed_file_extensions'])) { |
|
37 | + if ( ! is_array($options['allowed_file_extensions'])) { |
|
38 | 38 | throw new InvalidArgumentException(esc_html__('A valid allowed_file_extensions array was not provided to EE_File_Input', 'event_espresso')); |
39 | 39 | } |
40 | 40 | $this->allowed_file_extensions = $options['allowed_file_extensions']; |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | $this->allowed_file_extensions = ['csv']; |
43 | 43 | } |
44 | 44 | if (isset($options['allowed_mime_types'])) { |
45 | - if (!is_array($options['allowed_mime_types'])) { |
|
45 | + if ( ! is_array($options['allowed_mime_types'])) { |
|
46 | 46 | throw new InvalidArgumentException(esc_html__('A valid allowed_mime_types array was not provided to EE_File_Input', 'event_espresso')); |
47 | 47 | } |
48 | 48 | $this->allowed_mime_types = $options['allowed_file_extensions']; |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'), |
60 | 60 | implode(', ', $this->allowed_file_extensions) |
61 | 61 | ), |
62 | - '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~' |
|
62 | + '~.*\.('.implode('|', $this->allowed_file_extensions).')$~' |
|
63 | 63 | ) |
64 | 64 | ); |
65 | 65 | parent::__construct($options); |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | // $this->allowed_mime_types |
84 | 84 | // ) |
85 | 85 | array_map( |
86 | - function ($file_extension) { |
|
86 | + function($file_extension) { |
|
87 | 87 | return $file_extension; |
88 | 88 | }, |
89 | 89 | $this->allowed_file_extensions |
@@ -13,37 +13,37 @@ |
||
13 | 13 | class EE_File_Normalization extends EE_Normalization_Strategy_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Convert the $_FILES inputted data into a well-defined object. |
|
18 | - * @param string $value_to_normalize |
|
19 | - * @return FileSubmissionInterface |
|
20 | - */ |
|
21 | - public function normalize($value_to_normalize) |
|
22 | - { |
|
23 | - if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
24 | - return $value_to_normalize; |
|
25 | - } else { |
|
26 | - throw new EE_Validation_Error( |
|
27 | - esc_html__('The file input has an inavlid format.', 'event_espresso') |
|
28 | - ); |
|
29 | - } |
|
30 | - } |
|
16 | + /** |
|
17 | + * Convert the $_FILES inputted data into a well-defined object. |
|
18 | + * @param string $value_to_normalize |
|
19 | + * @return FileSubmissionInterface |
|
20 | + */ |
|
21 | + public function normalize($value_to_normalize) |
|
22 | + { |
|
23 | + if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) { |
|
24 | + return $value_to_normalize; |
|
25 | + } else { |
|
26 | + throw new EE_Validation_Error( |
|
27 | + esc_html__('The file input has an inavlid format.', 'event_espresso') |
|
28 | + ); |
|
29 | + } |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Convert the object back into a string of the filename. |
|
35 | - * |
|
36 | - * @param string $normalized_value |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function unnormalize($normalized_value) |
|
40 | - { |
|
41 | - if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
42 | - // Leave it as the object, it can be treated like a string because it |
|
43 | - // overrides __toString() |
|
44 | - return $normalized_value; |
|
45 | - } else { |
|
46 | - return (string) $normalized_value; |
|
47 | - } |
|
48 | - } |
|
33 | + /** |
|
34 | + * Convert the object back into a string of the filename. |
|
35 | + * |
|
36 | + * @param string $normalized_value |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function unnormalize($normalized_value) |
|
40 | + { |
|
41 | + if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) { |
|
42 | + // Leave it as the object, it can be treated like a string because it |
|
43 | + // overrides __toString() |
|
44 | + return $normalized_value; |
|
45 | + } else { |
|
46 | + return (string) $normalized_value; |
|
47 | + } |
|
48 | + } |
|
49 | 49 | } |