dbt-export.php
6 years ago
dbt-import.php
6 years ago
dop-export.php
6 years ago
dop-import.php
6 years ago
dpt-export.php
6 years ago
dpt-import.php
6 years ago
dt-export.php
6 years ago
dt-import.php
6 years ago
fg-local.php
6 years ago
dpt-import.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_post_types')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Import_DPT')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Import_DPT extends ACF_Admin_Tool{ |
| 13 | |
| 14 | function initialize(){ |
| 15 | |
| 16 | // vars |
| 17 | $this->name = 'acfe_tool_dpt_import'; |
| 18 | $this->title = __('Import Post Types'); |
| 19 | $this->icon = 'dashicons-upload'; |
| 20 | |
| 21 | } |
| 22 | |
| 23 | function html(){ |
| 24 | |
| 25 | ?> |
| 26 | <p><?php _e('Import Post Types', 'acf'); ?></p> |
| 27 | |
| 28 | <div class="acf-fields"> |
| 29 | <?php |
| 30 | |
| 31 | acf_render_field_wrap(array( |
| 32 | 'label' => __('Select File', 'acf'), |
| 33 | 'type' => 'file', |
| 34 | 'name' => 'acf_import_file', |
| 35 | 'value' => false, |
| 36 | 'uploader' => 'basic', |
| 37 | )); |
| 38 | |
| 39 | ?> |
| 40 | </div> |
| 41 | |
| 42 | <p class="acf-submit"> |
| 43 | <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button> |
| 44 | </p> |
| 45 | <?php |
| 46 | |
| 47 | } |
| 48 | |
| 49 | function submit(){ |
| 50 | |
| 51 | // Check file size. |
| 52 | if(empty($_FILES['acf_import_file']['size'])) |
| 53 | return acf_add_admin_notice(__("No file selected", 'acf'), 'warning'); |
| 54 | |
| 55 | // Get file data. |
| 56 | $file = $_FILES['acf_import_file']; |
| 57 | |
| 58 | // Check errors. |
| 59 | if($file['error']) |
| 60 | return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning'); |
| 61 | |
| 62 | // Check file type. |
| 63 | if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json') |
| 64 | return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning'); |
| 65 | |
| 66 | // Read JSON. |
| 67 | $json = file_get_contents($file['tmp_name']); |
| 68 | $json = json_decode($json, true); |
| 69 | |
| 70 | // Check if empty. |
| 71 | if(!$json || !is_array($json)) |
| 72 | return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning'); |
| 73 | |
| 74 | $ids = array(); |
| 75 | |
| 76 | $dynamic_post_types = get_option('acfe_dynamic_post_types', array()); |
| 77 | |
| 78 | // Loop over json |
| 79 | foreach($json as $post_type_name => $args){ |
| 80 | |
| 81 | // Check if already exists |
| 82 | if(isset($dynamic_post_types[$post_type_name])){ |
| 83 | |
| 84 | acf_add_admin_notice(__("Post type {$dynamic_post_types[$post_type_name]['label']} already exists. Import aborted."), 'warning'); |
| 85 | continue; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | // Vars |
| 90 | $title = $args['label']; |
| 91 | $name = $post_type_name; |
| 92 | |
| 93 | // Insert post |
| 94 | $post_id = wp_insert_post(array( |
| 95 | 'post_title' => $title, |
| 96 | 'post_name' => $name, |
| 97 | 'post_type' => 'acfe-dpt', |
| 98 | 'post_status' => 'publish' |
| 99 | )); |
| 100 | |
| 101 | // Insert error |
| 102 | if(is_wp_error($post_id)){ |
| 103 | |
| 104 | acf_add_admin_notice(__("Something went wrong with the post type {$title}. Import aborted."), 'warning'); |
| 105 | continue; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | // Register Args |
| 110 | update_field('acfe_dpt_name', $post_type_name, $post_id); |
| 111 | update_field('label', $args['label'], $post_id); |
| 112 | update_field('description', $args['description'], $post_id); |
| 113 | update_field('hierarchical', $args['hierarchical'], $post_id); |
| 114 | update_field('supports', $args['supports'], $post_id); |
| 115 | update_field('taxonomies', $args['taxonomies'], $post_id); |
| 116 | update_field('public', $args['public'], $post_id); |
| 117 | update_field('exclude_from_search', $args['exclude_from_search'], $post_id); |
| 118 | update_field('publicly_queryable', $args['publicly_queryable'], $post_id); |
| 119 | update_field('can_export', $args['can_export'], $post_id); |
| 120 | update_field('delete_with_user', $args['delete_with_user'], $post_id); |
| 121 | |
| 122 | // Labels |
| 123 | if(!empty($args['labels'])){ |
| 124 | |
| 125 | foreach($args['labels'] as $label_key => $label_value){ |
| 126 | |
| 127 | update_field('labels_' . $label_key, $label_value, $post_id); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | // Menu |
| 134 | update_field('menu_position', $args['menu_position'], $post_id); |
| 135 | update_field('menu_icon', $args['menu_icon'], $post_id); |
| 136 | update_field('show_ui', $args['show_ui'], $post_id); |
| 137 | update_field('show_in_menu', $args['show_in_menu'], $post_id); |
| 138 | update_field('show_in_nav_menus', $args['show_in_nav_menus'], $post_id); |
| 139 | update_field('show_in_admin_bar', $args['show_in_admin_bar'], $post_id); |
| 140 | |
| 141 | // Capability |
| 142 | update_field('capability_type', acf_encode_choices($args['capability_type'], false), $post_id); |
| 143 | update_field('map_meta_cap', $args['map_meta_cap'], $post_id); |
| 144 | |
| 145 | if(isset($args['capabilities'])) |
| 146 | update_field('capabilities', acf_encode_choices($args['capabilities'], false), $post_id); |
| 147 | |
| 148 | // Archive |
| 149 | update_field('acfe_dpt_archive_template', $args['acfe_archive_template'], $post_id); |
| 150 | update_field('acfe_dpt_archive_posts_per_page', $args['acfe_archive_ppp'], $post_id); |
| 151 | update_field('acfe_dpt_archive_orderby', $args['acfe_archive_orderby'], $post_id); |
| 152 | update_field('acfe_dpt_archive_order', $args['acfe_archive_order'], $post_id); |
| 153 | update_field('has_archive', $args['has_archive'], $post_id); |
| 154 | |
| 155 | // Single |
| 156 | update_field('acfe_dpt_single_template', $args['acfe_single_template'], $post_id); |
| 157 | update_field('rewrite', $args['rewrite'], $post_id); |
| 158 | |
| 159 | // Admin |
| 160 | update_field('acfe_dpt_admin_posts_per_page', $args['acfe_admin_ppp'], $post_id); |
| 161 | update_field('acfe_dpt_admin_orderby', $args['acfe_admin_orderby'], $post_id); |
| 162 | update_field('acfe_dpt_admin_order', $args['acfe_admin_order'], $post_id); |
| 163 | |
| 164 | // REST |
| 165 | update_field('show_in_rest', $args['show_in_rest'], $post_id); |
| 166 | update_field('rest_base', $args['rest_base'], $post_id); |
| 167 | update_field('rest_controller_class', $args['rest_controller_class'], $post_id); |
| 168 | |
| 169 | // Has archive: override |
| 170 | if($args['has_archive']) |
| 171 | update_field('has_archive_slug', $args['has_archive'], $post_id); |
| 172 | |
| 173 | // Rewrite: override |
| 174 | if($args['rewrite'] && is_array($args['rewrite'])){ |
| 175 | |
| 176 | update_field('rewrite', true, $post_id); |
| 177 | |
| 178 | update_field('rewrite_args_select', true, $post_id); |
| 179 | |
| 180 | update_field('rewrite_args_acfe_dpt_rewrite_slug', $args['rewrite']['slug'], $post_id); |
| 181 | update_field('rewrite_args_acfe_dpt_rewrite_with_front', $args['rewrite']['with_front'], $post_id); |
| 182 | update_field('rewrite_args_feeds', $args['rewrite']['feeds'], $post_id); |
| 183 | update_field('rewrite_args_pages', $args['rewrite']['pages'], $post_id); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | // Show in menu (text) |
| 188 | if($args['show_in_menu'] && is_string($args['show_in_menu'])) |
| 189 | update_field('show_in_menu_text', $args['show_in_menu'], $post_id); |
| 190 | |
| 191 | // Map meta cap |
| 192 | if($args['map_meta_cap'] === false) |
| 193 | update_field('map_meta_cap', 'false', $post_id); |
| 194 | |
| 195 | elseif($args['map_meta_cap'] === true) |
| 196 | update_field('map_meta_cap', 'true', $post_id); |
| 197 | |
| 198 | // Create ACFE option |
| 199 | $dynamic_post_types[$post_type_name] = $args; |
| 200 | |
| 201 | // Sort keys ASC |
| 202 | ksort($dynamic_post_types); |
| 203 | |
| 204 | // Update ACFE option |
| 205 | update_option('acfe_dynamic_post_types', $dynamic_post_types); |
| 206 | |
| 207 | // append message |
| 208 | $ids[] = $post_id; |
| 209 | |
| 210 | } |
| 211 | |
| 212 | if(empty($ids)) |
| 213 | return; |
| 214 | |
| 215 | // Count total |
| 216 | $total = count($ids); |
| 217 | |
| 218 | // Generate text |
| 219 | $text = sprintf(_n('1 post type imported', '%s post types imported', $total, 'acf'), $total); |
| 220 | |
| 221 | // Add links to text |
| 222 | $links = array(); |
| 223 | foreach($ids as $id){ |
| 224 | |
| 225 | $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>'; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | $text .= ': ' . implode(', ', $links); |
| 230 | |
| 231 | // Add notice |
| 232 | acf_add_admin_notice($text, 'success'); |
| 233 | |
| 234 | // Flush permalinks |
| 235 | flush_rewrite_rules(); |
| 236 | |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | acf_register_admin_tool('ACFE_Admin_Tool_Import_DPT'); |
| 242 | |
| 243 | endif; |