name = 'acfe_tool_dpt_export'; $this->title = __('Export Post Types'); $this->icon = 'dashicons-upload'; } function html(){ // vars $choices = array(); $dynamic_post_types = get_option('acfe_dynamic_post_types', array()); if($dynamic_post_types){ foreach($dynamic_post_types as $post_type_name => $args){ $choices[$post_type_name] = esc_html($args['label']); } } ?>
is_active() && acf_maybe_get_GET('keys')){ $this->submit(); } } function submit(){ $json = $this->get_selected(); // validate if($json === false) return acf_add_admin_notice(__('No post types selected'), 'warning'); $keys = array(); foreach($json as $key => $args){ $keys[] = $key; } // Prefix $prefix = (count($keys) > 1) ? 'post-types' : 'post-type'; // Slugs $slugs = implode('-', $keys); // Date $date = date('Y-m-d'); // file $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; // headers 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); die; } function get_selected(){ // vars $selected = $this->get_selected_keys(); $json = array(); if(!$selected) return false; $dynamic_post_types = get_option('acfe_dynamic_post_types', array()); if(empty($dynamic_post_types)) return false; // construct JSON foreach($selected as $key){ if(!isset($dynamic_post_types[$key])) continue; // add to json array $json[$key] = $dynamic_post_types[$key]; } // return return $json; } function get_selected_keys(){ // check $_POST if($keys = acf_maybe_get_POST('keys')) return (array) $keys; // check $_GET if($keys = acf_maybe_get_GET('keys')){ $keys = str_replace(' ', '+', $keys); return explode('+', $keys); } // return return false; } } acf_register_admin_tool('ACFE_Admin_Tool_Export_DPT'); endif;