Skip to content

Instantly share code, notes, and snippets.

@donaldallen
Created September 9, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldallen/6501160 to your computer and use it in GitHub Desktop.
Save donaldallen/6501160 to your computer and use it in GitHub Desktop.
<?php
/**
* Advanced Custom Fields: Import Export
*
* @package ACF_Import_Export
* @author Donald Allen <don@donaldallen.com>
* @link http://donaldallen.com
* @copyright 2013 Donald Allen
*/
class ACF_Import_Export {
protected $version = '1.0.0';
protected $plugin_slug = 'acf-import-export';
protected static $instance = null;
protected $plugin_screen_hook_suffix = null;
private function __construct()
{
add_action('admin_menu', array($this, 'add_plugin_admin_menu'));
// Load admin style sheet and JavaScript.
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
public static function get_instance()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public static function init()
{
global $messages;
global $acf_data;
global $choices;
$acfs = get_posts(array(
'numberposts' => -1,
'post_type' => 'acf',
'orderby' => 'menu_order title',
'order' => 'asc',
));
$choices = array();
if ($acfs) {
foreach($acfs as $acf) {
$title = apply_filters( 'the_title', $acf->post_title, $acf->ID );
$choices[$acf->ID] = $title;
}
}
session_start();
if (isset($_POST['import'])) {
self::import();
}
if (isset($_POST['export'])) {
self::export();
}
if ($_GET['import_agree']) {
print_r($_SESSION['acf-import-export']);
}
}
private function import()
{
global $messages;
global $acf_data;
if (isset($_FILES) && ! empty($_FILES)) {
if (pathinfo($_FILES['json_file']['name'], PATHINFO_EXTENSION) == 'json') {
$acf_data = json_decode(file_get_contents($_FILES['json_file']['tmp_name']), true);
} else {
$messages['error']['json'] = esc_html('You need to upload a JSON file.');
}
if (self::check_json()) {
$_SESSION['acf-import-export'] = $acf_data;
$acf_name = $acf_data['wp_posts']['post_title'];
$messages['success'] = 'You are about to import <strong>' . $acf_name . '</strong>. Are you sure? <a href="' . $_SERVER['REQUEST_URI'] . '&import_agree=yes" class="button">Yes</a> <a href="' . $_SERVER['REQUEST_URI'] . '" class="button">No, I made a terrible mistake!</a>';
}
}
}
public function export()
{
include_once (WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php');
$acf_posts = $_POST['acf_posts'];
foreach($acf_posts as $k => $v) {
$acfs = get_posts(array(
'numberposts' => -1,
'post_type' => 'acf',
'orderby' => 'menu_order title',
'order' => 'asc',
'include' => array($v),
'suppress_filters' => false,
));
foreach ($acfs as $i => $acf) {
$acf_data = array(
'id' => $acf->post_name,
'title' => $acf->post_title,
'fields' => apply_filters('acf/field_group/get_fields', array(), $acf->ID),
'location' => apply_filters('acf/field_group/get_location', array(), $acf->ID),
'options' => apply_filters('acf/field_group/get_options', array(), $acf->ID),
'menu_order' => $acf->menu_order,
);
$acf_data['fields'] = apply_filters('acf/export/clean_fields', $acf_data['fields']);
print_r($acf_data);
}
}
}
private function check_json()
{
global $messages;
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$messages['error']['json'] = esc_html('Maximum stack depth exceeded.');
break;
case JSON_ERROR_STATE_MISMATCH:
$messages['error']['json'] = esc_html('Underflow or the modes mismatch.');
break;
case JSON_ERROR_CTRL_CHAR:
$messages['error']['json'] = esc_html('Unexpected control character found.');
break;
case JSON_ERROR_SYNTAX:
$messages['error']['json'] = esc_html('Syntax error, malformed JSON.');
break;
case JSON_ERROR_UTF8:
$messages['error']['json'] = esc_html('Malformed UTF-8 characters, possibly incorrectly encoded.');
break;
}
if ( ! isset($messages['error'])) {
return true;
}
}
public function enqueue_admin_styles()
{
if ( ! isset($this->plugin_screen_hook_suffix)) {
return;
}
$screen = get_current_screen();
if ($screen->id == $this->plugin_screen_hook_suffix) {
wp_enqueue_style(
$this->plugin_slug .'-admin-styles',
plugins_url('assets/admin/css/admin.css', __FILE__),
array(),
$this->version
);
}
}
public function enqueue_admin_scripts()
{
if ( ! isset($this->plugin_screen_hook_suffix)) {
return;
}
$screen = get_current_screen();
if ($screen->id == $this->plugin_screen_hook_suffix) {
wp_enqueue_script(
$this->plugin_slug . '-admin-script',
plugins_url('assets/admin/js/admin.js', __FILE__),
array('jquery'),
$this->version
);
}
}
public function add_plugin_admin_menu()
{
$this->plugin_screen_hook_suffix = add_plugins_page(
'ACF Import Export',
'ACF Import Export',
'read',
$this->plugin_slug,
array($this, 'display_plugin_admin_page')
);
}
public function display_plugin_admin_page()
{
include_once('views/admin.php');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment