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
dop-import.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_options_pages')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Import_DOP')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Import_DOP extends ACF_Admin_Tool{ |
| 13 | |
| 14 | function initialize(){ |
| 15 | |
| 16 | // vars |
| 17 | $this->name = 'acfe_tool_dop_import'; |
| 18 | $this->title = __('Import Options Pages'); |
| 19 | $this->icon = 'dashicons-upload'; |
| 20 | |
| 21 | } |
| 22 | |
| 23 | function html(){ |
| 24 | |
| 25 | ?> |
| 26 | <p><?php _e('Import Options Pages', '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_options_pages = get_option('acfe_dynamic_options_pages', array()); |
| 77 | |
| 78 | $dynamic_options_sub_pages = array(); |
| 79 | |
| 80 | // Loop over json |
| 81 | foreach($json as $options_page_name => $args){ |
| 82 | |
| 83 | // Check if already exists |
| 84 | if(isset($dynamic_options_pages[$options_page_name])){ |
| 85 | |
| 86 | acf_add_admin_notice(__("Options page {$dynamic_options_pages[$options_page_name]['page_title']} already exists. Import aborted."), 'warning'); |
| 87 | continue; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | // Vars |
| 92 | $title = $args['page_title']; |
| 93 | $name = $options_page_name; |
| 94 | |
| 95 | // Insert post |
| 96 | $post_id = wp_insert_post(array( |
| 97 | 'post_title' => $title, |
| 98 | 'post_name' => $name, |
| 99 | 'post_type' => 'acfe-dop', |
| 100 | 'post_status' => 'publish' |
| 101 | )); |
| 102 | |
| 103 | // Insert error |
| 104 | if(is_wp_error($post_id)){ |
| 105 | |
| 106 | acf_add_admin_notice(__("Something went wrong with the options page {$title}. Import aborted."), 'warning'); |
| 107 | continue; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | // Register Args |
| 112 | update_field('page_title', $args['page_title'], $post_id); |
| 113 | update_field('acfe_dop_name', $name, $post_id); |
| 114 | update_field('menu_title', $args['menu_title'], $post_id); |
| 115 | update_field('menu_slug', $args['menu_slug'], $post_id); |
| 116 | update_field('capability', $args['capability'], $post_id); |
| 117 | update_field('position', $args['position'], $post_id); |
| 118 | update_field('parent_slug', $args['parent_slug'], $post_id); |
| 119 | update_field('icon_url', $args['icon_url'], $post_id); |
| 120 | update_field('redirect', $args['redirect'], $post_id); |
| 121 | update_field('post_id', $args['post_id'], $post_id); |
| 122 | update_field('autoload', $args['autoload'], $post_id); |
| 123 | update_field('update_button', $args['update_button'], $post_id); |
| 124 | update_field('updated_message', $args['updated_message'], $post_id); |
| 125 | |
| 126 | // Create ACFE option |
| 127 | $dynamic_options_pages[$options_page_name] = $args; |
| 128 | |
| 129 | // Sort keys ASC |
| 130 | ksort($dynamic_options_pages); |
| 131 | |
| 132 | // Update ACFE option |
| 133 | update_option('acfe_dynamic_options_pages', $dynamic_options_pages); |
| 134 | |
| 135 | // Append message |
| 136 | $ids[] = $post_id; |
| 137 | |
| 138 | // Add Sub Page |
| 139 | if(isset($args['parent_slug']) && !empty($args['parent_slug'])) |
| 140 | $dynamic_options_sub_pages[$post_id] = $args; |
| 141 | |
| 142 | } |
| 143 | |
| 144 | // Check if pages have been added |
| 145 | if(empty($ids)) |
| 146 | return; |
| 147 | |
| 148 | // Update Options Sub Pages |
| 149 | if(!empty($dynamic_options_sub_pages)){ |
| 150 | |
| 151 | foreach($dynamic_options_sub_pages as $post_id => $args){ |
| 152 | |
| 153 | // Get possible parent options pages |
| 154 | $get_dop_parent = get_posts(array( |
| 155 | 'post_type' => 'acfe-dop', |
| 156 | 'posts_per_page' => 1, |
| 157 | 'fields' => 'ids', |
| 158 | 'meta_query' => array( |
| 159 | array( |
| 160 | 'key' => 'menu_slug', |
| 161 | 'value' => $args['parent_slug'] |
| 162 | ) |
| 163 | ) |
| 164 | )); |
| 165 | |
| 166 | if(empty($get_dop_parent)) |
| 167 | continue; |
| 168 | |
| 169 | $parent = $get_dop_parent[0]; |
| 170 | |
| 171 | // Update sub page post |
| 172 | wp_update_post(array( |
| 173 | 'ID' => $post_id, |
| 174 | 'post_parent' => $parent, |
| 175 | )); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | // Count total |
| 182 | $total = count($ids); |
| 183 | |
| 184 | // Generate text |
| 185 | $text = sprintf(_n('1 options page imported', '%s options pages imported', $total, 'acf'), $total); |
| 186 | |
| 187 | // Add links to text |
| 188 | $links = array(); |
| 189 | foreach($ids as $id){ |
| 190 | |
| 191 | $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>'; |
| 192 | |
| 193 | } |
| 194 | |
| 195 | $text .= ': ' . implode(', ', $links); |
| 196 | |
| 197 | // Add notice |
| 198 | acf_add_admin_notice($text, 'success'); |
| 199 | |
| 200 | // Flush permalinks |
| 201 | flush_rewrite_rules(); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | acf_register_admin_tool('ACFE_Admin_Tool_Import_DOP'); |
| 208 | |
| 209 | endif; |