/** * Save post meta when the save_post action is called * * @since 1.0 * * @param int $post_id Popup (Post) ID * * @global array $post All the data of the the current post * @return void */ function popmake_popup_meta_box_save($post_id, $post) { if (isset($post->post_type) && 'popup' != $post->post_type) { return; } if (!isset($_POST['popmake_popup_meta_box_nonce']) || !wp_verify_nonce($_POST['popmake_popup_meta_box_nonce'], basename(__FILE__))) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || defined('DOING_AJAX') && DOING_AJAX || isset($_REQUEST['bulk_edit'])) { return; } if (isset($post->post_type) && 'revision' == $post->post_type) { return; } if (!current_user_can('edit_post', $post_id)) { return; } $field_prefix = Popmake_Popup_Fields::instance()->field_prefix; foreach (Popmake_Popup_Fields::instance()->get_all_fields() as $section => $fields) { $section_prefix = "{$field_prefix}{$section}"; $meta_values = array(); foreach ($fields as $field => $args) { $field_name = "{$section_prefix}_{$field}"; if (isset($_POST[$field_name])) { $meta_values[$field] = apply_filters('popmake_metabox_save_' . $field_name, $_POST[$field_name]); } } update_post_meta($post_id, "popup_{$section}", $meta_values); } foreach (popmake_popup_meta_fields() as $field) { if (isset($_POST[$field])) { $new = apply_filters('popmake_metabox_save_' . $field, $_POST[$field]); update_post_meta($post_id, $field, $new); } else { delete_post_meta($post_id, $field); } } $includes = popmake_get_popup_targeting_condition_includes($post_id); $excludes = popmake_get_popup_targeting_condition_excludes($post_id); foreach (popmake_get_supported_types() as $pt) { foreach (array('include', 'exclude') as $type) { $prefix = "popup_targeting_condition_" . ($type == 'exclude' ? 'exclude_' : '') . "on_{$pt}"; $current = $type == 'include' ? !empty($includes[$pt]) ? $includes[$pt] : array() : (!empty($excludes[$pt]) ? $excludes[$pt] : array()); $type_field = $prefix; $type_prefix = $prefix . '_'; $temp_ids = array(); if (!empty($_POST[$type_field])) { foreach (explode(',', trim($_POST[$type_field])) as $id) { if (is_int(intval($id))) { $temp_ids[] = intval($id); } } } /** * Remove existing meta that no longer exist in $_POST field. */ if (!empty($current)) { foreach ($current as $id) { if (!in_array($id, $temp_ids)) { delete_post_meta($post_id, $type_prefix . $id); } } } /** * Adds post meta for non existing post type ids in $_POST. */ foreach ($temp_ids as $id) { if (!in_array($id, $current) && $id > 0) { update_post_meta($post_id, $type_prefix . $id, true); } } } } do_action('popmake_save_popup', $post_id, $post); }
function popmake_register_popup_meta_fields($section, $fields = array()) { if (!empty($fields)) { Popmake_Popup_Fields::instance()->add_fields($section, $fields); } }