Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

WPEO_Upload_Class::get_post_data()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 2048
nop 1
dl 0
loc 18
rs 2.8
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * All methods utils for associate, dessociate and anothers things about upload.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0-alpha
7
 * @version 1.0.0
8
 * @copyright 2017-2018 Eoxia
9
 * @package EO_Framework\EO_Upload\Class
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\WPEO_Upload_Class' ) ) {
19
20
	/**
21
	 * All methods utils for associate, dessociate and anothers things about upload.
22
	 */
23
	class WPEO_Upload_Class extends \eoxia\Singleton_Util {
24
25
		/**
26
		 * Need to be declared for Singleton_Util.
27
		 *
28
		 * @since 0.1.0-alpha
29
		 * @version 0.1.0-alpha
30
		 */
31
		protected function construct() {}
32
33
		/**
34
		 * Get and sanitize $_POST data and return it.
35
		 *
36
		 * @since 1.0.0
37
		 * @version 1.0.0
38
		 *
39
		 * @param string $nonce_name Nonce name from the request AJAX.
40
		 *
41
		 * @return array $param {
42
		 *     Les variables renvoyées.
43
		 *
44
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
45
		 *     @type string  $title        The popup title.
46
		 *     @type string  $mode         Can be "edit" or "view".
47
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
48
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
49
		 *     @type string  $custom_class Add custom class.
50
		 *     @type string  $size         The size of the box (button for upload or open the gallery).
51
		 *     @type boolean $single       One media or more.
52
		 *     @type string  $mime_type    Can be application/document, application/png or empty for all mime types.
53
		 *     @type string  $display_type Can be box or list. By default box.
54
		 *     @type integer $file_id      The uploaded file ID.
55
		 * }
56
		 */
57
		public function get_post_data( $nonce_name ) {
58
			// check_ajax_referer( $nonce_name );
59
60
			$data                 = array();
61
			$data['id']           = ! empty( $_POST['id'] ) ? (int) $_POST['id'] : 0;
62
			$data['title']        = ! empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
63
			$data['mode']         = ! empty( $_POST['mode'] ) ? sanitize_text_field( $_POST['mode'] ) : '';
64
			$data['field_name']   = ! empty( $_POST['field_name'] ) ? sanitize_text_field( $_POST['field_name'] ) : '';
65
			$data['model_name']   = ! empty( $_POST['model_name'] ) ? stripslashes( sanitize_text_field( $_POST['model_name'] ) ) : '';
66
			$data['custom_class'] = ! empty( $_POST['custom_class'] ) ? stripslashes( sanitize_text_field( $_POST['custom_class'] ) ) : '';
67
			$data['size']         = ! empty( $_POST['size'] ) ? sanitize_text_field( $_POST['size'] ) : 'thumbnail';
68
			$data['single']       = ! empty( $_POST['single'] ) ? sanitize_text_field( $_POST['single'] ) : 'false';
69
			$data['mime_type']    = ! empty( $_POST['mime_type'] ) ? sanitize_text_field( $_POST['mime_type'] ) : '';
70
			$data['display_type'] = ! empty( $_POST['display_type'] ) ? sanitize_text_field( $_POST['display_type'] ) : '';
71
			$data['file_id']      = ! empty( $_POST['file_id'] ) ? (int) $_POST['file_id'] : 0;
72
73
			return $data;
74
		}
75
76
		/**
77
		 * Associate the file_id in the Object.
78
		 *
79
		 * @since 0.1.0-alpha
80
		 * @version 1.0.0
81
		 *
82
		 * @param array $data {
83
		 *     Les variables du tableau.
84
		 *
85
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
86
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
87
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
88
		 *     @type integer $file_id      The uploaded file ID.
89
		 * }
90
		 *
91
		 * @return mixed
92
		 */
93
		public function associate_file( $data ) {
94
			$element = null;
95
96
			if ( ! empty( $data['id'] ) ) {
97
				$element = $data['model_name']::g()->get( array(
98
					'id' => $data['id'],
99
				), true );
100
101
				$element->data['associated_document_id'][ $data['field_name'] ][] = (int) $data['file_id'];
102
				$data['model_name']::g()->update( $element->data );
103
			}
104
105
			return $element;
106
		}
107
108
		/**
109
		 * Dessociate the file_id in the Object.
110
		 *
111
		 * @since 0.1.0-alpha
112
		 * @version 1.0.0
113
		 *
114
		 * @param array $data {
115
		 *     Les variables du tableau.
116
		 *
117
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
118
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
119
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
120
		 *     @type integer $file_id      The uploaded file ID.
121
		 * }
122
		 *
123
		 * @return mixed
124
		 */
125
		public function dissociate_file( $data ) {
126
			$element = $data['model_name']::g()->get( array(
127
				'id' => $data['id'],
128
			), true );
129
130
			// Check if the file is in associated file list.
131
			if ( isset( $element->data['associated_document_id'] ) && isset( $element->data['associated_document_id'][ $data['field_name'] ] ) ) {
132
				$key = array_search( $data['file_id'], $element->data['associated_document_id'][ $data['field_name'] ], true );
133
				if ( false !== $key ) {
134
					array_splice( $element->data['associated_document_id'][ $data['field_name'] ], $key, 1 );
135
				}
136
			}
137
138
			// Check if the file is set as thumbnail.
139
			if ( $data['file_id'] === $element->data['thumbnail_id'] ) {
140
				$element->data['thumbnail_id'] = 0;
141
			}
142
143
			// Set another thumbnail id.
144
			if ( empty( $element->data['thumbnail_id'] ) && ! empty( $element->data['associated_document_id'][ $data['field_name'] ] ) ) {
145
				$element->data['thumbnail_id'] = $element->data['associated_document_id'][ $data['field_name'] ][0];
146
			}
147
148
			$data['model_name']::g()->update( $element->data );
149
150
			return $element;
151
		}
152
153
		/**
154
		 * Load and display the gallery.
155
		 *
156
		 * @since 0.1.0-alpha
157
		 * @version 1.0.0
158
		 *
159
		 * @param array $data {
160
		 *     Les variables du tableau.
161
		 *
162
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
163
		 *     @type string  $title        The popup title.
164
		 *     @type string  $mode         Can be "edit" or "view".
165
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
166
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
167
		 *     @type string  $custom_class Add custom class.
168
		 *     @type string  $size         The size of the box (button for upload or open the gallery).
169
		 *     @type boolean $single       One media or more.
170
		 *     @type string  $mime_type    Can be application/document, application/png or empty for all mime types.
171
		 *     @type string  $display_type Can be box or list. By default box.
172
		 *     @type integer $file_id      The uploaded file ID.
173
		 * }
174
		 *
175
		 * @return void
176
		 */
177
		public function display_gallery( $data ) {
178
			$element = $data['model_name']::g()->get( array(
179
				'id' => $data['id'],
180
			), true );
181
182
			$main_picture_id = $element->data['thumbnail_id'];
183
184
			if ( empty( $main_picture_id ) ) {
185
				$main_picture_id = $element->data['associated_document_id'][ $data['field_name'] ][0];
186
			}
187
188
			$list_id = ! empty( $element->data['associated_document_id'][ $data['field_name']  ] ) ? $element->data['associated_document_id'][ $data['field_name'] ] : array();
189
190
			require \eoxia\Config_Util::$init['eo-framework']->wpeo_upload->path . '/view/box/gallery/main.view.php';
191
		}
192
193
		/**
194
		 * Set the thumbnail.
195
		 *
196
		 * @since 0.1.0-alpha
197
		 * @version 1.0.0
198
		 *
199
		 * @param array $data {
200
		 *     Les variables du tableau.
201
		 *
202
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
203
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
204
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
205
		 *     @type integer $file_id      The uploaded file ID.
206
		 * }
207
		 *
208
		 * @return mixed
209
		 */
210
		public function set_thumbnail( $data ) {
211
			$element = $data['model_name']::g()->get( array( 'id' => $data['id'] ), true );
212
213
			$element->data['thumbnail_id'] = $data['file_id'];
214
215
			$element = $data['model_name']::g()->update( $element->data );
216
217
			return $element;
218
		}
219
220
		/**
221
		 * Output all attributes to send to the AJAX request.
222
		 *
223
		 * @since 1.0.0
224
		 * @version 1.0.0
225
		 *
226
		 * @param array $data {
227
		 *     Les variables du tableau.
228
		 *
229
		 *     @type integer $id           The id of the POST Element (Can be a custom post).
230
		 *     @type string  $title        The popup title.
231
		 *     @type string  $mode         Can be "edit" or "view".
232
		 *     @type string  $field_name   For use "_thumbnail_id" postmeta of WordPress let it empty.
233
		 *     @type string  $model_name   Say to WPEO_Model the model used. Write double slashes when use in shortcode. This method convert it from "//" to "\".
234
		 *     @type string  $custom_class Add custom class.
235
		 *     @type string  $size         The size of the box (button for upload or open the gallery).
236
		 *     @type boolean $single       One media or more.
237
		 *     @type string  $mime_type    Can be application/document, application/png or empty for all mime types.
238
		 *     @type string  $display_type Can be box or list. By default box.
239
		 *     @type integer $file_id      The uploaded file ID.
240
		 * }
241
		 *
242
		 * @return void
243
		 */
244
		public function out_all_attributes( $data ) {
245
			require \eoxia\Config_Util::$init['eo-framework']->wpeo_upload->path . '/view/box/gallery/attributes.view.php';
246
		}
247
	}
248
249
	WPEO_Upload_Class::g();
250
}
251