function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     /*
     // no need to check local groups because 
     // if they existed they would have been loaded by maybe_load_local
     // and we would not be here
     // will remove this code if the early loading of local json files works
     if (!$found) {
     	$json_path = plugin_dir_path(__FILE__).'acf-json';
     	if (!is_dir($json_path)) {
     		@mkdir($json_path);
     	}
     	if (is_multisite()) {
     		$json_path .= '/'.get_current_blog_id();
     		if (!is_dir($json_path)) {
     			@mkdir($json_path);
     		}
     	}
     	if (is_dir($json_path) && 
     			($files = scandir($json_path)) !== false &&
     			count($files)) {
     		foreach ($files as $file) {
     			$file_path = $json_path.'/'.$file;
     			if (!is_dir($file_path) && preg_match('/\.json$/', $file)) {
     				$file_group_key = preg_replace('/\.json$/', '', $file);
     				if ($file_group_key == $group_key) {
     					if (($json = file_get_contents($file_path)) !== false &&
     							($group = json_decode($json, true)) !== NULL) {
     						$json_found = true;
     						$found = true;
     					}
     				} // end if match
     			} // end if json file
     		} // end foreach file
     	} // end if is_dir etc
     } // end if !found
     */
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     // make sure json path exists
     $json_path = plugin_dir_path(__FILE__) . 'acf-json';
     if (!is_dir($json_path)) {
         @mkdir($json_path);
     }
     if (is_multisite()) {
         $json_path .= '/' . get_current_blog_id();
         if (!is_dir($json_path)) {
             @mkdir($json_path);
         }
     }
     // if json files was not found then save json file
     // attempt to save file/ silently fail if folder is not writable
     if (!$json_found && is_dir($json_path)) {
         if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
             $json = acf_json_encode($group);
             fwrite($handle, $json, strlen($json));
             fclose($handle);
         }
     }
     // end if !json found
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         //echo $group_key,'<br>';
         $this->clear_acf_cache();
         $acf_local = acf_local();
         unset($acf_local->groups[$group_key]);
         //echo '<pre>'; print_r($acf_local->groups); die;
         //unset(acf_local()->groups[$group_key]);
     }
     //echo '<pre>'; print_r($group); die;
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
     //echo $group_key,'<br><pre>'; print_r(acf_get_field_groups()); die;
 }
Example #2
0
function acf_remove_local_fields($parent)
{
    // bail early if no reference
    if (empty(acf_local()->parents[$parent])) {
        return false;
    }
    foreach (acf_local()->parents[$parent] as $key) {
        acf_remove_local_field($key);
    }
    // return
    return true;
}
function acf_get_local_fields($key)
{
    $fields = array();
    foreach (acf_local()->parents[$key] as $key) {
        $fields[] = acf_get_field($key);
    }
    return $fields;
}
Example #4
0
function acf_remove_local_fields($key = '')
{
    return acf_local()->remove_fields($key);
}
 function rebuild_group($group_key)
 {
     $group = $this->field_groups[$group_key];
     $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
     $group['fields'] = $this->replace_keys($group['fields']);
     unset($group['ID']);
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         unset(acf_local()->groups[$group_key]);
     }
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
 }
function acf_php_recovery_page()
{
    global $wpdb;
    $acf_local = acf_local();
    // process the form
    if (isset($_POST['acf_php_recovery_action']) && $_POST['acf_php_recovery_action'] == 'import' && isset($_POST['fieldsets']) && check_admin_referer('acf_php_recovery')) {
        $import_fieldsets = $_POST['fieldsets'];
        $imported = array();
        // Keep track of the imported
        $key_to_post_id = array();
        // Group or field key to post id
        // Now we can import the groups
        foreach ($acf_local->groups as $key => $group) {
            $group['title'] = $group['title'] . ' (Recovered)';
            // Only import those that were selected
            if (in_array($key, $import_fieldsets)) {
                $saved_group = acf_update_field_group($group);
                $key_to_post_id[$key] = $saved_group['ID'];
                // For displaying the success message
                $imported[] = array('title' => $group['title'], 'id' => $saved_group['ID']);
            }
        }
        // This requires multipile runs to handle sub-fields that have their parent set to the parent field instead of the group
        $field_parents = $import_fieldsets;
        // The groups and fields
        $imported_fields = array();
        // Keep track of the already imported
        do {
            $num_import = 0;
            foreach ($acf_local->fields as $key => $field) {
                if (!in_array($key, $imported_fields) && in_array($field['parent'], $field_parents)) {
                    $num_import = $num_import + 1;
                    $field_parents[] = $key;
                    $imported_fields[] = $key;
                    $field['parent'] = $key_to_post_id[$field['parent']];
                    // Convert the key into the post_parent
                    $saved_field = acf_update_field($field);
                    $key_to_post_id[$key] = $saved_field['ID'];
                }
            }
        } while ($num_import > 0);
    }
    // output
    ?>
  <div class="wrap">
  <h2>ACF PHP Recovery Tool</h2>

  <?php 
    // Check the version of ACF
    $acf_version = explode('.', acf_get_setting('version'));
    if ($acf_version[0] != '5') {
        ?>
  <div id="message" class="error below-h2">
    <p><?php 
        printf(__('This tool was built for ACF version 5 and you have version %s.'), $acf_version[0]);
        ?>
</p>
  </div>
  <?php 
    }
    ?>

  <?php 
    if (!empty($imported)) {
        ?>
      <div id="message" class="updated below-h2"><p><?php 
        _e('Fieldsets recovered');
        ?>
.</p>
      <ul>
      <?php 
        foreach ($imported as $import) {
            ?>
          <li><?php 
            edit_post_link($import['title'], '', '', $import['id']);
            ?>
</li>
          <?php 
        }
        ?>
        <li><strong><?php 
        _e('Remove the PHP defined fields! The duplicate field IDs interfer with the editing of the fields.');
        ?>
</strong></li>
      </ul>
      </div>
    <?php 
    }
    ?>

  <p><strong>This is a recovery tool. Do not use this as part of your workflow for importing and exporting ACF fieldsets.</strong></p>
  <form method="POST">
  <table class="widefat">
    <thead>
      <th>Import</th>
      <th>Name</th>
      <th>Possible Existing Matches</th>
    </thead>
    <tbody>
    <?php 
    foreach ($acf_local->groups as $key => $field_group) {
        ?>
    <tr>
      <td><input type="checkbox" name="fieldsets[]" value="<?php 
        echo esc_attr($key);
        ?>
" /></td>
      <td><?php 
        echo $field_group['title'];
        ?>
</td>
      <td><?php 
        $sql = "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_title LIKE '%{$field_group['title']}%' AND post_type='" . ACFPR_GROUP_POST_TYPE . "'";
        // Set post status
        $post_status = apply_filters('acf_recovery\\query\\post_status', '');
        if (!empty($post_status)) {
            $sql .= ' AND post_status="' . esc_sql($post_status) . '"';
        }
        $matches = $wpdb->get_results($sql);
        if (empty($matches)) {
            echo '<em>none</em>';
        } else {
            $links = array();
            foreach ($matches as $match) {
                $links[] = '<a href="' . get_edit_post_link($match->ID) . '">' . $match->post_title . '</a>';
            }
            echo implode(', ', $links);
        }
        ?>
</td>
    </tr>
    <?php 
    }
    ?>
    </tbody>
  </table>
    <?php 
    wp_nonce_field('acf_php_recovery');
    ?>
    <input type="hidden" name="acf_php_recovery_action" value="import" />
    <p class="submit">
      <input type="submit" value="Import" class="button-primary" />
    </p>
  </form>

  <h3>Registered Field Groups</h3>
  <pre class="">
  <?php 
    echo var_export($acf_local->groups);
    ?>
  </pre>

  </div>
  <?php 
}
Example #7
0
_e('Advanced Custom Fields Add-On', 'pmxi_plugin');
?>
</h3>	
		</div>
		<div class="wpallimport-collapsed-content" style="padding: 0;">
			<div class="wpallimport-collapsed-content-inner">
				<table class="form-table" style="max-width:none;">
					<tr>
						<td colspan="3">
							<?php 
global $acf;
$acfs = array();
//apply_filters('acf/get_field_groups', array());
if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0) {
    $saved_acfs = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field-group'));
    $acfs = acf_local()->groups;
} else {
    $acfs = apply_filters('acf/get_field_groups', array());
}
if (!empty($saved_acfs)) {
    foreach ($saved_acfs as $key => $obj) {
        $acfs[] = array('ID' => $obj->ID, 'title' => $obj->post_title);
    }
}
if (!empty($acfs)) {
    foreach ($acfs as $key => $acfObj) {
        if (empty($acfs[$key]['ID']) and !empty($acfs[$key]['key'])) {
            $acfs[$key]['ID'] = $acfs[$key]['key'];
        } elseif (empty($acfs[$key]['ID']) and !empty($acfs[$key]['id'])) {
            $acfs[$key]['ID'] = $acfs[$key]['id'];
        }
 /**
  * If ACF Pro is active then register a global for active fields - this provides legacy support to Landing Pages
  */
 public static function acf_register_global($field_group)
 {
     $GLOBALS['acf_register_field_group'][] = array('fields' => acf_local()->fields);
 }
 function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     if (!defined('ACF_REUSABLE_DISABLE_JSON') || !ACF_REUSABLE_DISABLE_JSON) {
         // make sure json path exists
         $json_path = plugin_dir_path(__FILE__) . 'acf-json';
         if (!is_dir($json_path)) {
             @mkdir($json_path);
         }
         if (is_multisite()) {
             $json_path .= '/' . get_current_blog_id();
             if (!is_dir($json_path)) {
                 @mkdir($json_path);
             }
         }
         // if json files was not found then save json file
         // attempt to save file/ silently fail if folder is not writable
         if (!$json_found && is_dir($json_path)) {
             if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
                 $json = acf_json_encode($group);
                 fwrite($handle, $json, strlen($json));
                 fclose($handle);
             }
         }
         // end if !json found
     }
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         //echo $group_key,'<br>';
         $this->clear_acf_cache();
         $acf_local = acf_local();
         unset($acf_local->groups[$group_key]);
         //echo '<pre>'; print_r($acf_local->groups); die;
         //unset(acf_local()->groups[$group_key]);
     }
     //echo '<pre>'; print_r($group); die;
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
     //echo $group_key,'<br><pre>'; print_r(acf_get_field_groups()); die;
 }
 function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     if (!$found) {
         $json_path = plugin_dir_path(__FILE__) . 'acf-json';
         if (is_dir($json_path) && ($files = scandir($json_path)) !== false && count($files)) {
             foreach ($files as $file) {
                 $file_path = $json_path . '/' . $file;
                 if (!is_dir($file_path) && preg_match('/\\.json$/', $file)) {
                     $file_group_key = preg_replace('/\\.json$/', '', $file);
                     if ($file_group_key == $group_key) {
                         if (($json = file_get_contents($file_path)) !== false && ($group = json_decode($json, true)) !== NULL) {
                             $json_found = true;
                             $found = true;
                         }
                     }
                     // end if match
                 }
                 // end if json file
             }
             // end foreach file
         }
         // end if is_dir etc
     }
     // end if !found
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     // if json files was not found then save json file
     // attempt to save file
     if (!$json_found && is_dir($json_path)) {
         if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
             $json = acf_json_encode($group);
             fwrite($handle, $json, strlen($json));
             fclose($handle);
         }
     }
     // end if !json found
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         unset(acf_local()->groups[$group_key]);
     }
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
 }
Example #11
0
function pmai_wp_ajax_get_acf()
{
    if (!check_ajax_referer('wp_all_import_secure', 'security', false)) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_import_plugin'))));
    }
    if (!current_user_can('manage_options')) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_import_plugin'))));
    }
    global $acf;
    $version = $acf ? $acf->settings['version'] : false;
    ob_start();
    $acf_groups = PMXI_Plugin::$session->acf_groups;
    $acf_obj = false;
    if (!empty($acf_groups)) {
        foreach ($acf_groups as $key => $group) {
            if ($group['ID'] == $_GET['acf']) {
                $acf_obj = $group;
                break;
            }
        }
    }
    $import = new PMXI_Import_Record();
    if (!empty($_GET['id'])) {
        $import->getById($_GET['id']);
    }
    $is_loaded_template = !empty(PMXI_Plugin::$session->is_loaded_template) ? PMXI_Plugin::$session->is_loaded_template : false;
    if ($is_loaded_template) {
        $default = PMAI_Plugin::get_default_import_options();
        $template = new PMXI_Template_Record();
        if (!$template->getById($is_loaded_template)->isEmpty()) {
            $options = (!empty($template->options) ? $template->options : array()) + $default;
        }
    } else {
        if (!$import->isEmpty()) {
            $options = $import->options;
        } else {
            $options = PMXI_Plugin::$session->options;
        }
    }
    ?>
	<div class="postbox  acf_postbox default acf_signle_group rad4" rel="<?php 
    echo $acf_obj['ID'];
    ?>
">
		<h3 class="hndle" style="margin-top:0;"><span><?php 
    echo $acf_obj['title'];
    ?>
</span></h3>
		<div class="inside">
		<?php 
    if ($version and version_compare($version, '5.0.0') >= 0) {
        if (is_numeric($acf_obj['ID'])) {
            $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $_GET['acf'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC'));
            if (!empty($acf_fields)) {
                foreach ($acf_fields as $field) {
                    $fieldData = !empty($field->post_content) ? unserialize($field->post_content) : array();
                    $fieldData['ID'] = $field->ID;
                    $fieldData['id'] = $field->ID;
                    $fieldData['label'] = $field->post_title;
                    $fieldData['key'] = $field->post_name;
                    if (empty($fieldData['name'])) {
                        $fieldData['name'] = $field->post_excerpt;
                    }
                    echo pmai_render_field($fieldData, !empty($options) ? $options : array());
                }
            }
        } else {
            $fields = acf_local()->fields;
            if (!empty($fields)) {
                foreach ($fields as $key => $field) {
                    if ($field['parent'] == $acf_obj['key']) {
                        $fieldData = $field;
                        $fieldData['id'] = uniqid();
                        $fieldData['label'] = $field['label'];
                        $fieldData['key'] = $field['key'];
                        echo pmai_render_field($fieldData, !empty($options) ? $options : array());
                    }
                }
            }
        }
    } else {
        if (is_numeric($acf_obj['ID'])) {
            $fields = array();
            foreach (get_post_meta($acf_obj['ID'], '') as $cur_meta_key => $cur_meta_val) {
                if (strpos($cur_meta_key, 'field_') !== 0) {
                    continue;
                }
                $fields[] = !empty($cur_meta_val[0]) ? unserialize($cur_meta_val[0]) : array();
            }
            if (count($fields)) {
                $sortArray = array();
                foreach ($fields as $field) {
                    foreach ($field as $key => $value) {
                        if (!isset($sortArray[$key])) {
                            $sortArray[$key] = array();
                        }
                        $sortArray[$key][] = $value;
                    }
                }
                $orderby = "order_no";
                array_multisort($sortArray[$orderby], SORT_ASC, $fields);
                foreach ($fields as $field) {
                    echo pmai_render_field($field, !empty($options) ? $options : array());
                }
            }
        } else {
            global $acf_register_field_group;
            if (!empty($acf_register_field_group)) {
                foreach ($acf_register_field_group as $key => $group) {
                    if ($group['id'] == $acf_obj['ID']) {
                        foreach ($group['fields'] as $field) {
                            echo pmai_render_field($field, !empty($options) ? $options : array());
                        }
                    }
                }
            }
        }
    }
    ?>
								
		</div>
	</div>
	<?php 
    exit(json_encode(array('html' => ob_get_clean())));
    die;
}
Example #12
0
 public function init(&$existing_meta_keys = array())
 {
     if (!class_exists('acf')) {
         return;
     }
     global $acf;
     if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0) {
         $saved_acfs = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field-group'));
         $acfs = acf_local()->groups;
         if (!empty($acfs) and is_array($acfs)) {
             $this->_acf_groups = $acfs;
         }
     } else {
         $this->_acf_groups = apply_filters('acf/get_field_groups', array());
     }
     if (!empty($saved_acfs)) {
         foreach ($saved_acfs as $key => $obj) {
             $this->_acf_groups[] = array('ID' => $obj->ID, 'title' => $obj->post_title);
         }
     }
     if (!empty($this->_acf_groups)) {
         foreach ($this->_acf_groups as $key => $acfObj) {
             if (empty($this->_acf_groups[$key]['ID']) and !empty($this->_acf_groups[$key]['key'])) {
                 $this->_acf_groups[$key]['ID'] = $acfs[$key]['key'];
             } elseif (empty($this->_acf_groups[$key]['ID']) and !empty($this->_acf_groups[$key]['id'])) {
                 $this->_acf_groups[$key]['ID'] = $this->_acf_groups[$key]['id'];
             }
         }
         // get all ACF fields
         if ($acf->settings['version'] and version_compare($acf->settings['version'], '5.0.0') >= 0) {
             foreach ($this->_acf_groups as $key => $acf_obj) {
                 if (is_numeric($acf_obj['ID'])) {
                     $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $acf_obj['ID'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC'));
                     if (!empty($acf_fields)) {
                         foreach ($acf_fields as $field) {
                             $fieldData = !empty($field->post_content) ? unserialize($field->post_content) : array();
                             $fieldData['ID'] = $field->ID;
                             $fieldData['id'] = $field->ID;
                             $fieldData['label'] = $field->post_title;
                             $fieldData['key'] = $field->post_name;
                             if (in_array($fieldData['type'], array('tab'))) {
                                 continue;
                             }
                             if (empty($fieldData['name'])) {
                                 $fieldData['name'] = $field->post_excerpt;
                             }
                             if (!empty($fieldData['name'])) {
                                 $this->_existing_acf_meta_keys[] = $fieldData['name'];
                             }
                             $this->_acf_groups[$key]['fields'][] = $fieldData;
                         }
                     }
                 } else {
                     $acf_fields = acf_local()->fields;
                     if (!empty($acf_fields)) {
                         foreach ($acf_fields as $field_key => $field) {
                             if ($field['parent'] == $acf_obj['key']) {
                                 $fieldData = $field;
                                 if (empty($fieldData['ID'])) {
                                     $fieldData['ID'] = $fieldData['id'] = uniqid();
                                 }
                                 if (!empty($fieldData['name'])) {
                                     $this->_existing_acf_meta_keys[] = $fieldData['name'];
                                 }
                                 $this->_acf_groups[$key]['fields'][] = $fieldData;
                             }
                         }
                     }
                 }
             }
         } else {
             foreach ($this->_acf_groups as $key => $acf_obj) {
                 $fields = array();
                 if (is_numeric($acf_obj['id'])) {
                     foreach (get_post_meta($acf_obj['id'], '') as $cur_meta_key => $cur_meta_val) {
                         if (strpos($cur_meta_key, 'field_') !== 0) {
                             continue;
                         }
                         $fields[] = !empty($cur_meta_val[0]) ? unserialize($cur_meta_val[0]) : array();
                     }
                 } else {
                     global $acf_register_field_group;
                     if (!empty($acf_register_field_group)) {
                         foreach ($acf_register_field_group as $group) {
                             if ($group['id'] == $acf_obj['ID']) {
                                 foreach ($group['fields'] as $field) {
                                     $fields[] = $field;
                                 }
                             }
                         }
                     }
                 }
                 if (count($fields)) {
                     $sortArray = array();
                     foreach ($fields as $field) {
                         foreach ($field as $key2 => $value) {
                             if (!isset($sortArray[$key2])) {
                                 $sortArray[$key2] = array();
                             }
                             $sortArray[$key2][] = $value;
                         }
                     }
                     $orderby = "order_no";
                     @array_multisort($sortArray[$orderby], SORT_ASC, $fields);
                     foreach ($fields as $field) {
                         if (in_array($field['type'], array('tab'))) {
                             continue;
                         }
                         $this->_acf_groups[$key]['fields'][] = $field;
                         if (!empty($field['name'])) {
                             $this->_existing_acf_meta_keys[] = $field['name'];
                         }
                     }
                 }
             }
         }
         if (!empty($existing_meta_keys)) {
             foreach ($existing_meta_keys as $key => $meta_key) {
                 foreach ($this->_existing_acf_meta_keys as $acf_key => $acf_value) {
                     if (in_array($meta_key, array($acf_value, "_" . $acf_value)) or strpos($meta_key, $acf_value) === 0 or strpos($meta_key, "_" . $acf_value) === 0) {
                         unset($existing_meta_keys[$key]);
                     }
                 }
             }
         }
     }
 }
Example #13
0
 /**
  * Perform import operation
  * @param string $xml XML string to import
  * @param callback[optional] $logger Method where progress messages are submmitted
  * @return pmai_Import_Record
  * @chainable
  */
 public function parse($parsing_data = array())
 {
     //$import, $count, $xml, $logger = NULL, $chunk = false, $xpath_prefix = ""
     $this->parsing_data = $parsing_data;
     add_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html'));
     kses_init();
     // do not perform special filtering for imported content
     $this->data = array();
     $records = array();
     $this->parsing_data['chunk'] == 1 and $this->parsing_data['logger'] and call_user_func($this->parsing_data['logger'], __('Composing advanced custom fields...', 'pmxi_plugin'));
     $acfs = $this->parsing_data['import']->options['acf'];
     global $acf;
     if (!empty($acfs)) {
         foreach ($acfs as $id => $status) {
             if (!$status) {
                 continue;
             }
             if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0) {
                 if (is_numeric($id)) {
                     $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $id, 'post_status' => 'publish'));
                     if (!empty($acf_fields)) {
                         foreach ($acf_fields as $field) {
                             $fieldData = !empty($field->post_content) ? unserialize($field->post_content) : array();
                             $fieldData['ID'] = $field->ID;
                             $fieldData['label'] = $field->post_title;
                             $fieldData['key'] = $field->post_name;
                             $fieldData['name'] = $field->post_excerpt;
                             if ($fieldData['type'] == 'flexible_content') {
                                 // vars
                                 $sub_fields = acf_get_fields($fieldData);
                                 // loop through layouts, sub fields and swap out the field key with the real field
                                 foreach (array_keys($fieldData['layouts']) as $i) {
                                     // extract layout
                                     $layout = acf_extract_var($fieldData['layouts'], $i);
                                     // append sub fields
                                     if (!empty($sub_fields)) {
                                         foreach (array_keys($sub_fields) as $k) {
                                             // check if 'parent_layout' is empty
                                             if (empty($sub_fields[$k]['parent_layout'])) {
                                                 // parent_layout did not save for this field, default it to first layout
                                                 $sub_fields[$k]['parent_layout'] = $layout['key'];
                                             }
                                             // append sub field to layout,
                                             if ($sub_fields[$k]['parent_layout'] == $layout['key']) {
                                                 $layout['sub_fields'][] = acf_extract_var($sub_fields, $k);
                                             }
                                         }
                                     }
                                     // append back to layouts
                                     $fieldData['layouts'][$i] = $layout;
                                 }
                             }
                             $this->data[$fieldData['key']] = $this->parse_field($fieldData, $this->parsing_data['import']->options['fields'][$fieldData['key']]);
                         }
                     }
                 } else {
                     $fields = acf_local()->fields;
                     if (!empty($fields)) {
                         foreach ($fields as $key => $field) {
                             if ($field['parent'] == $id) {
                                 $fieldData = $field;
                                 $fieldData['ID'] = uniqid();
                                 $fieldData['label'] = $field['label'];
                                 $fieldData['key'] = $field['key'];
                                 $fieldData['name'] = $field['name'];
                                 $this->data[$fieldData['key']] = $this->parse_field($fieldData, $this->parsing_data['import']->options['fields'][$fieldData['key']]);
                             }
                         }
                     }
                 }
             } else {
                 if (is_numeric($id)) {
                     foreach (get_post_meta($id, '') as $cur_meta_key => $cur_meta_val) {
                         if (strpos($cur_meta_key, 'field_') !== 0) {
                             continue;
                         }
                         $field = !empty($cur_meta_val[0]) ? unserialize($cur_meta_val[0]) : array();
                         $field_xpath = !empty($this->parsing_data['import']->options['fields'][$field['key']]) ? $this->parsing_data['import']->options['fields'][$field['key']] : "";
                         $this->data[$field['key']] = $this->parse_field($field, $field_xpath);
                     }
                 } else {
                     global $acf_register_field_group;
                     if (!empty($acf_register_field_group)) {
                         foreach ($acf_register_field_group as $key => $group) {
                             if ($group['id'] == $id) {
                                 foreach ($group['fields'] as $field) {
                                     $field_xpath = !empty($this->parsing_data['import']->options['fields'][$field['key']]) ? $this->parsing_data['import']->options['fields'][$field['key']] : "";
                                     $this->data[$field['key']] = $this->parse_field($field, $field_xpath);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     remove_filter('user_has_cap', array($this, '_filter_has_cap_unfiltered_html'));
     kses_init();
     // return any filtering rules back if they has been disabled for import procedure
     return $this->data;
 }