| Conditions | 36 |
| Paths | 1059 |
| Total Lines | 210 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |