initial commit
This commit is contained in:
@ -0,0 +1,578 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Admin_Tool_Export' ) ) :
|
||||
|
||||
class ACF_Admin_Tool_Export extends ACF_Admin_Tool {
|
||||
|
||||
/** @var string View context */
|
||||
var $view = '';
|
||||
|
||||
|
||||
/** @var array Export data */
|
||||
var $json = '';
|
||||
|
||||
|
||||
/**
|
||||
* initialize
|
||||
*
|
||||
* This function will initialize the admin tool
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function initialize() {
|
||||
|
||||
// vars
|
||||
$this->name = 'export';
|
||||
$this->title = __( 'Export Field Groups', 'acf' );
|
||||
|
||||
// active
|
||||
if ( $this->is_active() ) {
|
||||
$this->title .= ' - ' . __( 'Generate PHP', 'acf' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* submit
|
||||
*
|
||||
* This function will run when the tool's form has been submit
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function submit() {
|
||||
|
||||
// vars
|
||||
$action = acf_maybe_get_POST( 'action' );
|
||||
|
||||
// download
|
||||
if ( $action === 'download' ) {
|
||||
$this->submit_download();
|
||||
|
||||
// generate
|
||||
} elseif ( $action === 'generate' ) {
|
||||
$this->submit_generate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* submit_download
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @date 17/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function submit_download() {
|
||||
|
||||
// vars
|
||||
$json = $this->get_selected();
|
||||
|
||||
// validate
|
||||
if ( $json === false ) {
|
||||
return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
// headers
|
||||
$file_name = 'acf-export-' . date( 'Y-m-d' ) . '.json';
|
||||
header( 'Content-Description: File Transfer' );
|
||||
header( "Content-Disposition: attachment; filename={$file_name}" );
|
||||
header( 'Content-Type: application/json; charset=utf-8' );
|
||||
|
||||
// return
|
||||
echo acf_json_encode( $json ) . "\r\n";
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* submit_generate
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @date 17/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function submit_generate() {
|
||||
|
||||
// vars
|
||||
$keys = $this->get_selected_keys();
|
||||
|
||||
// validate
|
||||
if ( ! $keys ) {
|
||||
return acf_add_admin_notice( __( 'No field groups selected', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
// url
|
||||
$url = add_query_arg( 'keys', implode( '+', $keys ), $this->get_url() );
|
||||
|
||||
// redirect
|
||||
wp_redirect( $url );
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* load
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @date 21/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function load() {
|
||||
|
||||
// active
|
||||
if ( $this->is_active() ) {
|
||||
|
||||
// get selected keys
|
||||
$selected = $this->get_selected_keys();
|
||||
|
||||
// add notice
|
||||
if ( $selected ) {
|
||||
$count = count( $selected );
|
||||
$text = sprintf( _n( 'Exported 1 item.', 'Exported %s items.', $count, 'acf' ), $count );
|
||||
acf_add_admin_notice( $text, 'success' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* html
|
||||
*
|
||||
* This function will output the metabox HTML
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function html() {
|
||||
|
||||
// single (generate PHP)
|
||||
if ( $this->is_active() ) {
|
||||
$this->html_single();
|
||||
|
||||
// archive
|
||||
} else {
|
||||
$this->html_archive();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Renders the checkboxes to select items to export.
|
||||
*
|
||||
* @date 24/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function html_field_selection() {
|
||||
// Ensure `l10n_var_export` is always false at the point we're outputting the options.
|
||||
acf_update_setting( 'l10n_var_export', false );
|
||||
// Reset the field-groups store which may have been corrupted by export.
|
||||
$store = acf_get_store( 'field-groups' );
|
||||
if ( $store ) {
|
||||
$store->reset();
|
||||
}
|
||||
|
||||
$choices = array();
|
||||
$selected = $this->get_selected_keys();
|
||||
$field_groups = acf_get_internal_post_type_posts( 'acf-field-group' );
|
||||
|
||||
if ( $field_groups ) {
|
||||
foreach ( $field_groups as $field_group ) {
|
||||
$choices[ $field_group['key'] ] = esc_html( $field_group['title'] );
|
||||
}
|
||||
}
|
||||
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Select Field Groups', 'acf' ),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'keys',
|
||||
'prefix' => false,
|
||||
'value' => $selected,
|
||||
'toggle' => true,
|
||||
'choices' => $choices,
|
||||
)
|
||||
);
|
||||
|
||||
$choices = array();
|
||||
$selected = $this->get_selected_keys();
|
||||
$post_types = acf_get_internal_post_type_posts( 'acf-post-type' );
|
||||
|
||||
if ( $post_types ) {
|
||||
foreach ( $post_types as $post_type ) {
|
||||
$choices[ $post_type['key'] ] = esc_html( $post_type['title'] );
|
||||
}
|
||||
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Select Post Types', 'acf' ),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'post_type_keys',
|
||||
'prefix' => false,
|
||||
'value' => $selected,
|
||||
'toggle' => true,
|
||||
'choices' => $choices,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$choices = array();
|
||||
$selected = $this->get_selected_keys();
|
||||
$taxonomies = acf_get_internal_post_type_posts( 'acf-taxonomy' );
|
||||
|
||||
if ( $taxonomies ) {
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
$choices[ $taxonomy['key'] ] = esc_html( $taxonomy['title'] );
|
||||
}
|
||||
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Select Taxonomies', 'acf' ),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'taxonomy_keys',
|
||||
'prefix' => false,
|
||||
'value' => $selected,
|
||||
'toggle' => true,
|
||||
'choices' => $choices,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$choices = array();
|
||||
$selected = $this->get_selected_keys();
|
||||
$options_pages = acf_get_internal_post_type_posts( 'acf-ui-options-page' );
|
||||
|
||||
if ( $options_pages ) {
|
||||
foreach ( $options_pages as $options_page ) {
|
||||
$choices[ $options_page['key'] ] = esc_html( $options_page['title'] );
|
||||
}
|
||||
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Select Options Pages', 'acf' ),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'ui_options_page_keys',
|
||||
'prefix' => false,
|
||||
'value' => $selected,
|
||||
'toggle' => true,
|
||||
'choices' => $choices,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the side panel for selecting ACF items to export via PHP.
|
||||
*
|
||||
* @date 21/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function html_panel_selection() {
|
||||
?>
|
||||
<div class="acf-panel acf-panel-selection">
|
||||
<?php $this->html_field_selection(); ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* html_panel_settings
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @date 21/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function html_panel_settings() {
|
||||
|
||||
?>
|
||||
<div class="acf-panel acf-panel-settings">
|
||||
<h3 class="acf-panel-title"><?php _e( 'Settings', 'acf' ); ?> <i class="dashicons dashicons-arrow-right"></i></h3>
|
||||
<div class="acf-panel-inside">
|
||||
<?php
|
||||
|
||||
/*
|
||||
acf_render_field_wrap(array(
|
||||
'label' => __('Empty settings', 'acf'),
|
||||
'type' => 'select',
|
||||
'name' => 'minimal',
|
||||
'prefix' => false,
|
||||
'value' => '',
|
||||
'choices' => array(
|
||||
'all' => __('Include all settings', 'acf'),
|
||||
'minimal' => __('Ignore empty settings', 'acf'),
|
||||
)
|
||||
));
|
||||
*/
|
||||
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* html_archive
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @date 20/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function html_archive() {
|
||||
|
||||
?>
|
||||
<div class="acf-postbox-header">
|
||||
<h2 class="acf-postbox-title"><?php esc_html_e( 'Export', 'acf' ); ?></h2>
|
||||
<div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the items you would like to export and then select your export method. Export As JSON to export to a .json file which you can then import to another ACF installation. Generate PHP to export to PHP code which you can place in your theme.', 'acf' ); ?>">?</i></div>
|
||||
</div>
|
||||
<div class="acf-postbox-inner">
|
||||
<div class="acf-fields">
|
||||
<?php $this->html_field_selection(); ?>
|
||||
</div>
|
||||
<p class="acf-submit acf-actions-strip">
|
||||
<button type="submit" name="action" class="acf-btn acf-button-primary" value="download"><?php _e( 'Export As JSON', 'acf' ); ?></button>
|
||||
<button type="submit" name="action" class="acf-btn acf-btn-secondary" value="generate"><?php _e( 'Generate PHP', 'acf' ); ?></button>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the PHP export screen.
|
||||
*
|
||||
* @date 20/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function html_single() {
|
||||
?>
|
||||
<div class="acf-postbox-header">
|
||||
<h2 class="acf-postbox-title"><?php esc_html_e( 'Export - Generate PHP', 'acf' ); ?></h2>
|
||||
<i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( "The following code can be used to register a local version of the selected items. Storing field groups, post types, or taxonomies locally can provide many benefits such as faster load times, version control & dynamic fields/settings. Simply copy and paste the following code to your theme's functions.php file or include it within an external file, then deactivate or delete the items from the ACF admin.", 'acf' ); ?>">?</i>
|
||||
</div>
|
||||
<div class="acf-postbox-columns">
|
||||
<div class="acf-postbox-main">
|
||||
<?php $this->html_generate(); ?>
|
||||
</div>
|
||||
<div class="acf-postbox-side">
|
||||
<?php $this->html_panel_selection(); ?>
|
||||
<p class="acf-submit">
|
||||
<button type="submit" name="action" class="acf-btn" value="generate"><?php esc_html_e( 'Generate PHP', 'acf' ); ?></button>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the HTML for the PHP export functionality.
|
||||
*
|
||||
* @date 17/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function html_generate() {
|
||||
// Prevent default translation and fake __() within string.
|
||||
acf_update_setting( 'l10n_var_export', true );
|
||||
|
||||
$json = $this->get_selected();
|
||||
$to_export = array();
|
||||
|
||||
// Sort by ACF post type first so we can wrap them in related functions.
|
||||
foreach ( $json as $post ) {
|
||||
$post_type = acf_determine_internal_post_type( $post['key'] );
|
||||
|
||||
if ( $post_type ) {
|
||||
$to_export[ $post_type ][] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
echo '<textarea id="acf-export-textarea" readonly="readonly">';
|
||||
|
||||
foreach ( $to_export as $post_type => $posts ) {
|
||||
if ( 'acf-field-group' === $post_type ) {
|
||||
echo "add_action( 'acf/include_fields', function() {\r\n";
|
||||
echo "\tif ( ! function_exists( 'acf_add_local_field_group' ) ) {\r\n\t\treturn;\r\n\t}\r\n\r\n";
|
||||
} elseif ( 'acf-post-type' === $post_type || 'acf-taxonomy' === $post_type ) {
|
||||
echo "add_action( 'init', function() {\r\n";
|
||||
} elseif ( 'acf-ui-options-page' === $post_type ) {
|
||||
echo "add_action( 'acf/init', function() {\r\n";
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
foreach ( $posts as $post ) {
|
||||
if ( $count !== 0 ) {
|
||||
echo "\r\n";
|
||||
}
|
||||
|
||||
echo "\t" . acf_export_internal_post_type_as_php( $post, $post_type ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_textarea() used earlier.
|
||||
++$count;
|
||||
}
|
||||
|
||||
if ( in_array( $post_type, array( 'acf-post-type', 'acf-taxonomy', 'acf-field-group', 'acf-ui-options-page' ), true ) ) {
|
||||
echo "} );\r\n\r\n";
|
||||
}
|
||||
|
||||
if ( 'acf-post-type' === $post_type ) {
|
||||
echo acf_export_enter_title_here( $posts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_textarea() used earlier.
|
||||
}
|
||||
}
|
||||
|
||||
echo '</textarea>';
|
||||
?>
|
||||
<p class="acf-submit">
|
||||
<a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a>
|
||||
</p>
|
||||
<script type="text/javascript">
|
||||
(function($){
|
||||
const $a = $('#acf-export-copy');
|
||||
const $textarea = $('#acf-export-textarea');
|
||||
|
||||
// Remove $a if 'copy' is not supported.
|
||||
if( !document.queryCommandSupported('copy') ) {
|
||||
return $a.remove();
|
||||
}
|
||||
|
||||
$a.on('click', function( e ){
|
||||
e.preventDefault();
|
||||
|
||||
$textarea.get(0).select();
|
||||
|
||||
try {
|
||||
var copy = document.execCommand('copy');
|
||||
if ( ! copy ) {
|
||||
return;
|
||||
}
|
||||
|
||||
acf.newTooltip({
|
||||
text: "<?php esc_html_e( 'Copied', 'acf' ); ?>",
|
||||
timeout: 250,
|
||||
target: $(this),
|
||||
});
|
||||
} catch (err) {
|
||||
// Do nothing.
|
||||
}
|
||||
});
|
||||
})(jQuery);
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of keys that have been selected in the export tool.
|
||||
*
|
||||
* @date 20/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function get_selected_keys() {
|
||||
$key_names = array( 'keys', 'taxonomy_keys', 'post_type_keys', 'ui_options_page_keys' );
|
||||
$all_keys = array();
|
||||
|
||||
foreach ( $key_names as $key_name ) {
|
||||
if ( $keys = acf_maybe_get_POST( $key_name ) ) {
|
||||
$all_keys = array_merge( $all_keys, (array) $keys );
|
||||
} elseif ( $keys = acf_maybe_get_GET( $key_name ) ) {
|
||||
$keys = str_replace( ' ', '+', $keys );
|
||||
$keys = explode( '+', $keys );
|
||||
$all_keys = array_merge( $all_keys, (array) $keys );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $all_keys ) ) {
|
||||
return $all_keys;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JSON data for given $_POST args.
|
||||
*
|
||||
* @date 17/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function get_selected() {
|
||||
$selected = $this->get_selected_keys();
|
||||
$json = array();
|
||||
|
||||
if ( ! $selected ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $selected as $key ) {
|
||||
$post_type = acf_determine_internal_post_type( $key );
|
||||
$post = acf_get_internal_post_type( $key, $post_type );
|
||||
|
||||
if ( empty( $post ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'acf-field-group' === $post_type ) {
|
||||
$post['fields'] = acf_get_fields( $post );
|
||||
}
|
||||
|
||||
$post = acf_prepare_internal_post_type_for_export( $post, $post_type );
|
||||
$json[] = $post;
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
|
||||
// initialize
|
||||
acf_register_admin_tool( 'ACF_Admin_Tool_Export' );
|
||||
endif; // class_exists check
|
||||
|
||||
?>
|
@ -0,0 +1,298 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Admin_Tool_Import' ) ) :
|
||||
|
||||
class ACF_Admin_Tool_Import extends ACF_Admin_Tool {
|
||||
|
||||
/**
|
||||
* initialize
|
||||
*
|
||||
* This function will initialize the admin tool
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function initialize() {
|
||||
|
||||
// vars
|
||||
$this->name = 'import';
|
||||
$this->title = __( 'Import Field Groups', 'acf' );
|
||||
$this->icon = 'dashicons-upload';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* html
|
||||
*
|
||||
* This function will output the metabox HTML
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function html() {
|
||||
|
||||
?>
|
||||
<div class="acf-postbox-header">
|
||||
<h2 class="acf-postbox-title"><?php esc_html_e( 'Import', 'acf' ); ?></h2>
|
||||
<div class="acf-tip"><i tabindex="0" class="acf-icon acf-icon-help acf-js-tooltip" title="<?php esc_attr_e( 'Select the Advanced Custom Fields JSON file you would like to import. When you click the import button below, ACF will import the items in that file.', 'acf' ); ?>">?</i></div>
|
||||
</div>
|
||||
<div class="acf-postbox-inner">
|
||||
<div class="acf-fields">
|
||||
<?php
|
||||
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Select File', 'acf' ),
|
||||
'type' => 'file',
|
||||
'name' => 'acf_import_file',
|
||||
'value' => false,
|
||||
'uploader' => 'basic',
|
||||
)
|
||||
);
|
||||
|
||||
?>
|
||||
</div>
|
||||
<p class="acf-submit">
|
||||
<button type="submit" class="acf-btn" name="import_type" value="json">
|
||||
<?php esc_html_e( 'Import JSON', 'acf' ); ?>
|
||||
</button>
|
||||
</p>
|
||||
|
||||
<?php
|
||||
if ( is_plugin_active( 'custom-post-type-ui/custom-post-type-ui.php' ) && acf_get_setting( 'enable_post_types' ) ) {
|
||||
$cptui_post_types = get_option( 'cptui_post_types' );
|
||||
$cptui_taxonomies = get_option( 'cptui_taxonomies' );
|
||||
$choices = array();
|
||||
$overwrite_warning = false;
|
||||
|
||||
if ( $cptui_post_types ) {
|
||||
$choices['post_types'] = __( 'Post Types', 'acf' );
|
||||
$existing_post_types = acf_get_acf_post_types();
|
||||
|
||||
foreach ( $existing_post_types as $existing_post_type ) {
|
||||
if ( isset( $cptui_post_types[ $existing_post_type['post_type'] ] ) ) {
|
||||
$overwrite_warning = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $cptui_taxonomies ) {
|
||||
$choices['taxonomies'] = __( 'Taxonomies', 'acf' );
|
||||
|
||||
if ( ! $overwrite_warning ) {
|
||||
$existing_taxonomies = acf_get_acf_taxonomies();
|
||||
foreach ( $existing_taxonomies as $existing_taxonomy ) {
|
||||
if ( isset( $cptui_taxonomies[ $existing_taxonomy['taxonomy'] ] ) ) {
|
||||
$overwrite_warning = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $choices ) ) :
|
||||
?>
|
||||
<div class="acf-fields import-cptui">
|
||||
<?php
|
||||
acf_render_field_wrap(
|
||||
array(
|
||||
'label' => __( 'Import from Custom Post Type UI', 'acf' ),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'acf_import_cptui',
|
||||
'choices' => $choices,
|
||||
'toggle' => true,
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
if ( $overwrite_warning ) {
|
||||
echo '<p class="acf-inline-notice notice notice-info">' . esc_html__( 'Importing a Post Type or Taxonomy with the same key as one that already exists will overwrite the settings for the existing Post Type or Taxonomy with those of the import.', 'acf' ) . '</p>';
|
||||
}
|
||||
?>
|
||||
<p class="acf-submit">
|
||||
<button type="submit" class="acf-btn" name="import_type" value="cptui">
|
||||
<?php esc_html_e( 'Import from Custom Post Type UI', 'acf' ); ?>
|
||||
</button>
|
||||
</p>
|
||||
<?php
|
||||
endif;
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports the selected ACF posts and returns an admin notice on completion.
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @return ACF_Admin_Notice
|
||||
*/
|
||||
public function submit() {
|
||||
//phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce verified before this function is called.
|
||||
if ( 'cptui' === acf_request_arg( 'import_type', '' ) ) {
|
||||
$import = acf_sanitize_request_args( $_POST['acf_import_cptui'] );
|
||||
return $this->import_cpt_ui( $import );
|
||||
}
|
||||
|
||||
// Check file size.
|
||||
if ( empty( $_FILES['acf_import_file']['size'] ) ) {
|
||||
return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
$file = acf_sanitize_files_array( $_FILES['acf_import_file'] );
|
||||
|
||||
// Check errors.
|
||||
if ( $file['error'] ) {
|
||||
return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
// Check file type.
|
||||
if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
|
||||
return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
// Read JSON.
|
||||
$json = file_get_contents( $file['tmp_name'] );
|
||||
$json = json_decode( $json, true );
|
||||
|
||||
// Check if empty.
|
||||
if ( ! $json || ! is_array( $json ) ) {
|
||||
return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
// Ensure $json is an array of posts.
|
||||
if ( isset( $json['key'] ) ) {
|
||||
$json = array( $json );
|
||||
}
|
||||
|
||||
// Remember imported post ids.
|
||||
$ids = array();
|
||||
|
||||
// Loop over json.
|
||||
foreach ( $json as $to_import ) {
|
||||
// Search database for existing post.
|
||||
$post_type = acf_determine_internal_post_type( $to_import['key'] );
|
||||
$post = acf_get_internal_post_type_post( $to_import['key'], $post_type );
|
||||
|
||||
if ( $post ) {
|
||||
$to_import['ID'] = $post->ID;
|
||||
}
|
||||
|
||||
// Import the post.
|
||||
$to_import = acf_import_internal_post_type( $to_import, $post_type );
|
||||
|
||||
// Append message.
|
||||
$ids[] = $to_import['ID'];
|
||||
}
|
||||
|
||||
// Count number of imported posts.
|
||||
$total = count( $ids );
|
||||
|
||||
// Generate text.
|
||||
$text = sprintf( _n( 'Imported 1 item', 'Imported %s items', $total, 'acf' ), $total );
|
||||
|
||||
// Add links to text.
|
||||
$links = array();
|
||||
foreach ( $ids as $id ) {
|
||||
$links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
|
||||
}
|
||||
$text .= ' ' . implode( ', ', $links );
|
||||
|
||||
// Add notice.
|
||||
return acf_add_admin_notice( $text, 'success' );
|
||||
//phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the import of CPTUI post types and taxonomies.
|
||||
*
|
||||
* @since 6.1
|
||||
*
|
||||
* @param array $import_args What to import.
|
||||
* @return ACF_Admin_Notice
|
||||
*/
|
||||
public function import_cpt_ui( $import_args ) {
|
||||
if ( ! is_array( $import_args ) ) {
|
||||
return acf_add_admin_notice( __( 'Nothing from Custom Post Type UI plugin selected for import.', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
$imported = array();
|
||||
|
||||
// Import any post types.
|
||||
if ( in_array( 'post_types', $import_args, true ) ) {
|
||||
$cptui_post_types = get_option( 'cptui_post_types' );
|
||||
$instance = acf_get_internal_post_type_instance( 'acf-post-type' );
|
||||
|
||||
if ( ! is_array( $cptui_post_types ) || ! $instance ) {
|
||||
return acf_add_admin_notice( __( 'Failed to import post types.', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
foreach ( $cptui_post_types as $post_type ) {
|
||||
$result = $instance->import_cptui_post_type( $post_type );
|
||||
|
||||
if ( is_array( $result ) && isset( $result['ID'] ) ) {
|
||||
$imported[] = (int) $result['ID'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Import any taxonomies.
|
||||
if ( in_array( 'taxonomies', $import_args, true ) ) {
|
||||
$cptui_taxonomies = get_option( 'cptui_taxonomies' );
|
||||
$instance = acf_get_internal_post_type_instance( 'acf-taxonomy' );
|
||||
|
||||
if ( ! is_array( $cptui_taxonomies ) || ! $instance ) {
|
||||
return acf_add_admin_notice( __( 'Failed to import taxonomies.', 'acf' ), 'warning' );
|
||||
}
|
||||
|
||||
foreach ( $cptui_taxonomies as $taxonomy ) {
|
||||
$result = $instance->import_cptui_taxonomy( $taxonomy );
|
||||
|
||||
if ( is_array( $result ) && isset( $result['ID'] ) ) {
|
||||
$imported[] = (int) $result['ID'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $imported ) ) {
|
||||
// Generate text.
|
||||
$total = count( $imported );
|
||||
/* translators: %d - number of items imported from CPTUI */
|
||||
$text = sprintf( _n( 'Imported %d item from Custom Post Type UI -', 'Imported %d items from Custom Post Type UI -', $total, 'acf' ), $total );
|
||||
|
||||
// Add links to text.
|
||||
$links = array();
|
||||
foreach ( $imported as $id ) {
|
||||
$links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>';
|
||||
}
|
||||
|
||||
$text .= ' ' . implode( ', ', $links );
|
||||
$text .= __( '. The Custom Post Type UI plugin can be deactivated.', 'acf' );
|
||||
|
||||
return acf_add_admin_notice( $text, 'success' );
|
||||
}
|
||||
|
||||
return acf_add_admin_notice( __( 'Nothing to import', 'acf' ), 'warning' );
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize.
|
||||
acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
|
||||
endif; // class_exists check.
|
||||
|
||||
?>
|
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Admin_Tool' ) ) :
|
||||
|
||||
class ACF_Admin_Tool {
|
||||
|
||||
|
||||
/** @var string Tool name */
|
||||
var $name = '';
|
||||
|
||||
|
||||
/** @var string Tool title */
|
||||
var $title = '';
|
||||
|
||||
|
||||
/** @var string Dashicon slug */
|
||||
var $icon = '';
|
||||
|
||||
|
||||
/** @var boolean Redirect form to single */
|
||||
// var $redirect = false;
|
||||
|
||||
|
||||
/**
|
||||
* get_name
|
||||
*
|
||||
* This function will return the Tool's name
|
||||
*
|
||||
* @date 19/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function get_name() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get_title
|
||||
*
|
||||
* This function will return the Tool's title
|
||||
*
|
||||
* @date 19/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function get_title() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get_url
|
||||
*
|
||||
* This function will return the Tool's title
|
||||
*
|
||||
* @date 19/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function get_url() {
|
||||
return acf_get_admin_tool_url( $this->name );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* is_active
|
||||
*
|
||||
* This function will return true if the tool is active
|
||||
*
|
||||
* @date 19/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
function is_active() {
|
||||
return acf_maybe_get_GET( 'tool' ) === $this->name;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* __construct
|
||||
*
|
||||
* This function will setup the class functionality
|
||||
*
|
||||
* @type function
|
||||
* @date 27/6/17
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function __construct() {
|
||||
|
||||
// initialize
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* initialize
|
||||
*
|
||||
* This function will initialize the admin tool
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function initialize() {
|
||||
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* load
|
||||
*
|
||||
* This function is called during the admin page load
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function load() {
|
||||
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* html
|
||||
*
|
||||
* This function will output the metabox HTML
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function html() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* submit
|
||||
*
|
||||
* This function will run when the tool's form has been submit
|
||||
*
|
||||
* @date 10/10/17
|
||||
* @since 5.6.3
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function submit() {
|
||||
}
|
||||
}
|
||||
|
||||
endif; // class_exists check
|
Reference in New Issue
Block a user