예제 #1
0
 /**
  * Cleans post meta on removal of dynamic fields
  *
  */
 function sanitize_data($input)
 {
     if (!empty($_POST)) {
         // If we are updating the options.php page with dynamic fields
         if ($_POST['option_page'] == $this->id && !empty($this->post_type)) {
             $post_meta_keys = array();
             $option_keys = array();
             $dynamic_fields = $_POST[$this->id];
             $args = array('post_type' => $this->post_type);
             $posts = Cuztom::get_cuztom_posts($args);
             $prefix = 'nm_dyn_';
             // Add the posted option key names
             foreach ($dynamic_fields as $value) {
                 foreach ($value as $post_value) {
                     $key_name = $post_value['_field_name'];
                     $option_keys[] = Cuztom::uglify($prefix . $key_name);
                 }
             }
             if (!empty($posts)) {
                 // Loop through all the posts deleting dynamic_fields that arn't used anymore
                 foreach ($posts as $post) {
                     // Get the post meta for all the posts + add it to an array
                     $post_metas = get_post_custom($post->ID);
                     foreach ($post_metas as $key => $value) {
                         if (strpos($key, 'nm_dyn_') !== false) {
                             $post_meta_keys[$key] = false;
                         }
                     }
                     // Check against the posted option values and set to true
                     foreach ($post_meta_keys as $post_meta_key => $value) {
                         foreach ($option_keys as $option_key) {
                             if (strpos($post_meta_key, $option_key) !== false) {
                                 $post_meta_keys[$post_meta_key] = true;
                             }
                         }
                     }
                     // If the post_meta failed the check and isn't being used delete it
                     foreach ($post_meta_keys as $post_meta_key => $is_used) {
                         if ($is_used == false) {
                             delete_post_meta($post->ID, $post_meta_key);
                         }
                     }
                 }
             }
         }
     }
     return $input;
 }