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
dbt-import.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_block_types')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Import_DBT')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Import_DBT extends ACF_Admin_Tool{ |
| 13 | |
| 14 | function initialize(){ |
| 15 | |
| 16 | // vars |
| 17 | $this->name = 'acfe_tool_dbt_import'; |
| 18 | $this->title = __('Import Block Types'); |
| 19 | $this->icon = 'dashicons-upload'; |
| 20 | |
| 21 | } |
| 22 | |
| 23 | function html(){ |
| 24 | |
| 25 | ?> |
| 26 | <p><?php _e('Import Block 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_block_types = get_option('acfe_dynamic_block_types', array()); |
| 77 | |
| 78 | // Loop over json |
| 79 | foreach($json as $block_type_name => $args){ |
| 80 | |
| 81 | // Check if already exists |
| 82 | if(isset($dynamic_block_types[$block_type_name])){ |
| 83 | |
| 84 | acf_add_admin_notice(__("Block type {$dynamic_block_types[$block_type_name]['title']} already exists. Import aborted."), 'warning'); |
| 85 | continue; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | // Vars |
| 90 | $title = $args['title']; |
| 91 | $name = $block_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-dbt', |
| 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 block type {$title}. Import aborted."), 'warning'); |
| 105 | continue; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | // Register Args |
| 110 | update_field('name', $name, $post_id); |
| 111 | update_field('title', $args['title'], $post_id); |
| 112 | update_field('description', $args['description'], $post_id); |
| 113 | update_field('category', $args['category'], $post_id); |
| 114 | update_field('keywords', acf_encode_choices($args['keywords'], false), $post_id); |
| 115 | update_field('post_types', $args['post_types'], $post_id); |
| 116 | update_field('mode', $args['mode'], $post_id); |
| 117 | update_field('align', $args['align'], $post_id); |
| 118 | update_field('render_callback', $args['render_callback'], $post_id); |
| 119 | update_field('enqueue_assets', $args['enqueue_assets'], $post_id); |
| 120 | |
| 121 | // Render Template |
| 122 | if(!empty($args['render_template'])) |
| 123 | update_field('render_template', str_replace(ACFE_THEME_PATH . '/', '', $args['render_template']), $post_id); |
| 124 | |
| 125 | // Enqueue Style |
| 126 | if(!empty($args['enqueue_style'])) |
| 127 | update_field('enqueue_style', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_style']), $post_id); |
| 128 | |
| 129 | // Enqueue Script |
| 130 | if(!empty($args['enqueue_script'])) |
| 131 | update_field('enqueue_script', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_script']), $post_id); |
| 132 | |
| 133 | // Align |
| 134 | if(empty($args['align'])) |
| 135 | update_field('align', 'none', $post_id); |
| 136 | |
| 137 | // Icon |
| 138 | if(!empty($args['icon'])){ |
| 139 | |
| 140 | // Simple |
| 141 | if(is_string($args['icon'])){ |
| 142 | |
| 143 | update_field('icon_type', 'simple', $post_id); |
| 144 | |
| 145 | update_field('icon_text', $args['icon'], $post_id); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | // Colors |
| 150 | elseif(is_array($args['icon'])){ |
| 151 | |
| 152 | update_field('icon_type', 'colors', $post_id); |
| 153 | |
| 154 | update_field('icon_background', $args['icon']['background'], $post_id); |
| 155 | update_field('icon_foreground', $args['icon']['foreground'], $post_id); |
| 156 | update_field('icon_src', $args['icon']['src'], $post_id); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | // Supports: Align |
| 163 | update_field('supports_align', $args['supports']['align'], $post_id); |
| 164 | |
| 165 | if(is_array($args['supports']['align'])){ |
| 166 | |
| 167 | update_field('supports_align_args', acf_encode_choices($args['supports']['align'], false), $post_id); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | // Supports: Mode |
| 172 | update_field('supports_mode', $args['supports']['mode'], $post_id); |
| 173 | |
| 174 | // Supports: Multiple |
| 175 | update_field('supports_multiple', $args['supports']['multiple'], $post_id); |
| 176 | |
| 177 | // Create ACFE option |
| 178 | $dynamic_block_types[$block_type_name] = $args; |
| 179 | |
| 180 | // Sort keys ASC |
| 181 | ksort($dynamic_block_types); |
| 182 | |
| 183 | // Update ACFE option |
| 184 | update_option('acfe_dynamic_block_types', $dynamic_block_types); |
| 185 | |
| 186 | // append message |
| 187 | $ids[] = $post_id; |
| 188 | |
| 189 | } |
| 190 | |
| 191 | if(empty($ids)) |
| 192 | return; |
| 193 | |
| 194 | // Count total |
| 195 | $total = count($ids); |
| 196 | |
| 197 | // Generate text |
| 198 | $text = sprintf(_n('1 block type imported', '%s block types imported', $total, 'acf'), $total); |
| 199 | |
| 200 | // Add links to text |
| 201 | $links = array(); |
| 202 | foreach($ids as $id){ |
| 203 | |
| 204 | $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>'; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | $text .= ': ' . implode(', ', $links); |
| 209 | |
| 210 | // Add notice |
| 211 | acf_add_admin_notice($text, 'success'); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | } |
| 216 | |
| 217 | acf_register_admin_tool('ACFE_Admin_Tool_Import_DBT'); |
| 218 | |
| 219 | endif; |