Completed
Push — master ( 42ba23...5fbbab )
by Julien
03:55
created

functions-metabox.php ➔ wpbo_save_custom_fields()   F

Complexity

Conditions 36
Paths 1059

Size

Total Lines 210
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 36
eloc 80
c 3
b 0
f 1
nc 1059
nop 1
dl 0
loc 210
rs 2

How to fix   Long Method    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
 * BetterOptin Metabox
4
 *
5
 * @package   BetterOptin/Metabox
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
add_action( 'add_meta_boxes', 'wpbo_register_metabox_steps' );
18
/**
19
 * Popup Design Step.
20
 *
21
 * This popup requires additional checks so
22
 * we use a completely custom one.
23
 *
24
 * @since  1.0.0
25
 */
26
function wpbo_register_metabox_steps() {
27
	add_meta_box( 'wpbo_step1_template', __( 'Step 1: Choose a Template', 'betteroptin' ), 'wpbo_display_metabox_step1', 'wpbo-popup', 'normal', 'default' );
28
	add_meta_box( 'wpbo_step2_settings', __( 'Step 2: Edit Settings', 'betteroptin' ), 'wpbo_display_metabox_step2', 'wpbo-popup', 'normal', 'default' );
29
	add_meta_box( 'wpbo_step3_display', __( 'Step 3: Choose where to Display', 'betteroptin' ), 'wpbo_display_metabox_step3', 'wpbo-popup', 'normal', 'default' );
30
	add_meta_box( 'wpbo_step4_design', __( 'Step 4: Customize the Design', 'betteroptin' ), 'wpbo_display_metabox_step4', 'wpbo-popup', 'normal', 'default' );
31
	add_meta_box( 'wpbo_step5_checklist', __( 'Checklist', 'betteroptin' ), 'wpbo_display_metabox_step5', 'wpbo-popup', 'side', 'high' );
32
}
33
34
/**
35
 * Display the content of step 1 metabox.
36
 *
37
 * @since  1.0.0
38
 */
39
function wpbo_display_metabox_step1() {
40
	include_once( WPBO_PATH . 'includes/admin/views/metaboxes/template.php' );
41
}
42
43
/**
44
 * Display the content of step 2 metabox.
45
 *
46
 * @since  1.0.0
47
 */
48
function wpbo_display_metabox_step2() {
49
	include_once( WPBO_PATH . 'includes/admin/views/metaboxes/settings.php' );
50
}
51
52
/**
53
 * Display the content of step 3 metabox.
54
 *
55
 * @since  1.0.0
56
 */
57
function wpbo_display_metabox_step3() {
58
	include_once( WPBO_PATH . 'includes/admin/views/metaboxes/display.php' );
59
}
60
61
/**
62
 * Display the content of step 3 metabox.
63
 *
64
 * @since  1.0.0
65
 */
66
function wpbo_display_metabox_step4() {
67
	include_once( WPBO_PATH . 'includes/admin/views/metaboxes/design.php' );
68
}
69
70
/**
71
 * Display the content of step 3 metabox.
72
 *
73
 * @since  1.0.0
74
 */
75
function wpbo_display_metabox_step5() {
76
	include_once( WPBO_PATH . 'includes/admin/views/metaboxes/checklist.php' );
77
}
78
79
add_action( 'save_post', 'wpbo_save_custom_fields' );
80
/**
81
 * Save custom fields.
82
 *
83
 * Save the complex custom fields that can't be handled
84
 * through TItan Framework. This includes step 3: where
85
 * the popup should be displayed.
86
 *
87
 * @param int $post_id Current post ID
88
 *
89
 * @since  1.0.0
90
 */
91
function wpbo_save_custom_fields( $post_id ) {
92
93
	if ( ! isset( $_POST['wpbo_display'] ) || isset( $_POST['wpbo_display'] ) && ! wp_verify_nonce( $_POST['wpbo_display'], 'add_display' ) ) {
94
		return;
95
	}
96
97
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
98
		return;
99
	}
100
101
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
102
		return;
103
	}
104
105
	/**
106
	 * STEP 1: Template
107
	 */
108
	if ( isset( $_POST['wpbo_template'] ) ) {
109
110
		$previous_template = get_post_meta( $post_id, 'wpbo_template', true );
111
		update_post_meta( $post_id, 'wpbo_template', $_POST['wpbo_template'], $previous_template );
112
113
		/* Delete possible customizations */
114
		if ( $previous_template != $_POST['wpbo_template'] ) {
115
			delete_post_meta( $post_id, '_wpbo_template_editor' );
116
			delete_post_meta( $post_id, '_wpbo_template_display' );
117
		}
118
119
	}
120
121
	/**
122
	 * STEP 2: Settings
123
	 */
124
	$settings = array();
125
	$step2    = array(
126
		'close_overlay',
127
		'close_esc',
128
		'hide_close_button',
129
		'cookie_lifetime',
130
		'animation',
131
		'close_button',
132
		'overlay_color',
133
		'overlay_opacity',
134
		'wiggle',
135
		'return_url',
136
	);
137
138
	if ( isset( $_POST['wpbo_settings'] ) ) {
139
140
		foreach ( $step2 as $option ) {
141
142
			if ( isset( $_POST['wpbo_settings'][ $option ] ) ) {
143
144
				$settings[ $option ] = $_POST['wpbo_settings'][ $option ];
145
146
			}
147
148
		}
149
150
		update_post_meta( $post_id, '_wpbo_settings', $settings );
151
152
	}
153
154
	/**
155
	 * STEP 3: Display
156
	 */
157
158
	/**
159
	 * Display everywhere.
160
	 *
161
	 * This is the simplest case: just display the popup
162
	 * on every single page that is loaded.
163
	 */
164
	if ( isset( $_POST['wpbo_display_all'] ) ) {
165
		update_post_meta( $post_id, '_wpbo_display_all', 'yes' );
166
	} else {
167
		update_post_meta( $post_id, '_wpbo_display_all', 'no' );
168
	}
169
170
	/* Get available public post types */
171
	$post_types = get_post_types( array( 'public' => true ) );
172
	$except     = array( 'attachment', 'wpbo-popup' );
173
174
	/* Get popup / posts relationships */
175
	$relationships = get_option( 'wpbo_popup_relationships', array() );
176
177
	/**
178
	 * Handle each post individually.
179
	 */
180
	foreach ( $post_types as $key => $pt ) {
181
182
		/* Exclude specific post types */
183
		if ( in_array( $key, $except ) ) {
184
			continue;
185
		}
186
187
		/* Current value */
188
		$current = get_post_meta( $post_id, '_wpbo_display_' . $pt, true );
189
190
		/* Set $current at the correct format if needed */
191
		if ( ! is_array( $current ) ) {
192
			$current = array();
193
		}
194
195
		if ( isset( $_POST[ 'wpbo_display_' . $pt ] ) ) {
196
197
			$display = $_POST[ 'wpbo_display_' . $pt ];
198
199
			if ( isset( $_POST[ 'wpbo_display_' . $pt . '_all' ] ) ) {
200
				$display = 'all';
201
			}
202
203
			/**
204
			 * Create the relationships
205
			 */
206
			if ( is_array( $display ) ) {
207
208
				foreach ( $display as $key => $post ) {
209
210
					/**
211
					 * Check if a relationship already exists for this post.
212
					 * If there is one, we remove it from the post meta itself
213
					 * as relationships are also stored in the post metas (so that
214
					 * we can populate the fields).
215
					 */
216
					if ( isset( $relationships[ $post ] ) && $post_id != $relationships[ $post ] ) {
217
218
						$old = $edit = get_post_meta( $relationships[ $post ], '_wpbo_display_' . $pt, true );
219
220
						if ( is_array( $edit ) && ( $relation_key = array_search( $post, $edit ) ) !== false ) {
221
222
							unset( $edit[ $relation_key ] );
223
224
							update_post_meta( $relationships[ $post ], '_wpbo_display_' . $pt, $edit, $old );
225
						}
226
227
					}
228
229
					/* Add the new relationship */
230
					$relationships[ $post ] = $post_id;
231
				}
232
233
				/**
234
				 * Clean previous relationships that might have been removed.
235
				 */
236
				$diff = array_diff( $current, $display );
237
238
				foreach ( $diff as $dkey => $dval ) {
239
					if ( isset( $relationships[ $dval ] ) ) {
240
						unset( $relationships[ $dval ] );
241
					}
242
				}
243
244
				/**
245
				 * Update the relationships
246
				 */
247
				update_option( 'wpbo_popup_relationships', $relationships );
248
249
			}
250
251
			if ( maybe_serialize( $current ) != maybe_serialize( $display ) ) {
252
				update_post_meta( $post_id, '_wpbo_display_' . $pt, $display );
253
			}
254
255
		} elseif ( ! isset( $_POST[ 'wpbo_display_' . $pt ] ) ) {
256
257
			$prev = get_post_meta( $post_id, '_wpbo_display_' . $pt, true );
258
259
			if ( isset( $_POST[ 'wpbo_display_' . $pt . '_all' ] ) && 'all' != $current ) {
260
				update_post_meta( $post_id, '_wpbo_display_' . $pt, 'all' );
261
			} elseif ( ! isset( $_POST[ 'wpbo_display_' . $pt . '_all' ] ) && '' != $current ) {
262
				delete_post_meta( $post_id, '_wpbo_display_' . $pt );
263
			}
264
265
			/**
266
			 * Remove the relationships
267
			 */
268
			if ( is_array( $prev ) ) {
269
270
				foreach ( $prev as $pid ) {
271
272
					if ( isset( $relationships[ $pid ] ) ) {
273
						unset( $relationships[ $pid ] );
274
					}
275
276
				}
277
278
				update_option( 'wpbo_popup_relationships', $relationships );
279
280
			}
281
282
		}
283
284
	}
285
286
	/* Redirect to customizer */
287
	if ( isset( $_POST['save_customize'] ) ) {
288
289
		$customizer = add_query_arg( array( 'post_type'  => 'wpbo-popup',
290
		                                    'page'       => 'wpbo-customizer',
291
		                                    'wpbo_popup' => $post_id
292
		), admin_url( 'edit.php' ) );
293
294
		wp_redirect( $customizer );
295
296
		exit;
297
298
	}
299
300
}