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
fg-local.php
321 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('ACFE_Admin_Tool_FG_Local_Export')): |
| 7 | |
| 8 | class ACFE_Admin_Tool_FG_Local extends ACF_Admin_Tool{ |
| 9 | |
| 10 | function initialize(){ |
| 11 | |
| 12 | // vars |
| 13 | $this->title = __('Export Local Field Groups'); |
| 14 | $this->name = 'acfe-fg-local'; |
| 15 | $this->icon = 'dashicons-upload'; |
| 16 | |
| 17 | } |
| 18 | |
| 19 | function load(){ |
| 20 | |
| 21 | if($ids = acf_maybe_get_GET('acfe-fg-local-sync')){ |
| 22 | |
| 23 | $ids = explode('+', $ids); |
| 24 | |
| 25 | // Count number of imported field groups. |
| 26 | $total = count($ids); |
| 27 | |
| 28 | // Generate text. |
| 29 | $text = sprintf( _n( 'Imported 1 field group', 'Imported %s field groups', $total, 'acf' ), $total ); |
| 30 | |
| 31 | // Add links to text. |
| 32 | $links = array(); |
| 33 | foreach( $ids as $id ) { |
| 34 | $links[] = '<a href="' . get_edit_post_link( $id ) . '">' . get_the_title( $id ) . '</a>'; |
| 35 | } |
| 36 | $text .= ' ' . implode( ', ', $links ); |
| 37 | |
| 38 | // Add notice |
| 39 | acf_add_admin_notice($text, 'success'); |
| 40 | |
| 41 | } |
| 42 | |
| 43 | if($this->is_active()){ |
| 44 | |
| 45 | $array = $this->get_selected(); |
| 46 | $keys = $this->get_selected_keys(); |
| 47 | $action = $this->get_action(); |
| 48 | |
| 49 | // validate |
| 50 | if($array === false) |
| 51 | return acf_add_admin_notice(__('No field group selected'), 'warning'); |
| 52 | |
| 53 | // Json |
| 54 | if($action === 'json'){ |
| 55 | |
| 56 | // Slugs |
| 57 | $slugs = implode('-', $keys); |
| 58 | |
| 59 | // Date |
| 60 | $date = date('Y-m-d'); |
| 61 | |
| 62 | // file |
| 63 | $file_name = 'acfe-export-local-' . $slugs . '-' . $date . '.json'; |
| 64 | |
| 65 | // headers |
| 66 | header("Content-Description: File Transfer"); |
| 67 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 68 | header("Content-Type: application/json; charset=utf-8"); |
| 69 | |
| 70 | // return |
| 71 | echo acf_json_encode($array); |
| 72 | die; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | // Sync |
| 77 | elseif($action === 'sync'){ |
| 78 | |
| 79 | if(isset($array['key'])) |
| 80 | $array = array($array); |
| 81 | |
| 82 | // Remeber imported field group ids. |
| 83 | $ids = array(); |
| 84 | |
| 85 | // Loop over json |
| 86 | foreach( $array as $field_group ) { |
| 87 | |
| 88 | // Search database for existing field group. |
| 89 | $post = acf_get_field_group_post( $field_group['key'] ); |
| 90 | if( $post ) { |
| 91 | $field_group['ID'] = $post->ID; |
| 92 | } |
| 93 | |
| 94 | // Import field group. |
| 95 | $field_group = acf_import_field_group( $field_group ); |
| 96 | |
| 97 | // append message |
| 98 | $ids[] = $field_group['ID']; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | // url |
| 103 | $url = add_query_arg('acfe-fg-local-sync', implode('+', $ids), acf_get_admin_tools_url()); |
| 104 | |
| 105 | // redirect |
| 106 | wp_redirect($url); |
| 107 | exit; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | function html(){ |
| 116 | |
| 117 | if($this->is_active()){ |
| 118 | |
| 119 | $array = $this->get_selected(); |
| 120 | $action = $this->get_action(); |
| 121 | |
| 122 | ?> |
| 123 | <div class="acf-postbox-columns"> |
| 124 | <div class="acf-postbox-main"> |
| 125 | |
| 126 | <?php |
| 127 | // prevent default translation and fake __() within string |
| 128 | acf_update_setting('l10n_var_export', true); |
| 129 | |
| 130 | // vars |
| 131 | $json = $array; |
| 132 | |
| 133 | $str_replace = array( |
| 134 | " " => "\t", |
| 135 | "'!!__(!!\'" => "__('", |
| 136 | "!!\', !!\'" => "', '", |
| 137 | "!!\')!!'" => "')", |
| 138 | "array (" => "array(" |
| 139 | ); |
| 140 | |
| 141 | $preg_replace = array( |
| 142 | '/([\t\r\n]+?)array/' => 'array', |
| 143 | '/[0-9]+ => array/' => 'array' |
| 144 | ); |
| 145 | |
| 146 | |
| 147 | ?> |
| 148 | <p><?php _e("The following code can be used to register a local version of the selected field group(s). A local field group 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.", 'acf'); ?></p> |
| 149 | |
| 150 | <div id="acf-admin-tool-export"> |
| 151 | |
| 152 | <textarea id="acf-export-textarea" readonly="true"><?php |
| 153 | |
| 154 | echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n"; |
| 155 | |
| 156 | foreach( $json as $field_group ) { |
| 157 | |
| 158 | // code |
| 159 | $code = var_export($field_group, true); |
| 160 | |
| 161 | |
| 162 | // change double spaces to tabs |
| 163 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 164 | |
| 165 | |
| 166 | // correctly formats "=> array(" |
| 167 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 168 | |
| 169 | |
| 170 | // esc_textarea |
| 171 | $code = esc_textarea( $code ); |
| 172 | |
| 173 | |
| 174 | // echo |
| 175 | echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n"; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | echo "endif;"; |
| 180 | |
| 181 | ?></textarea> |
| 182 | |
| 183 | </div> |
| 184 | |
| 185 | <p class="acf-submit"> |
| 186 | <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a> |
| 187 | </p> |
| 188 | <script type="text/javascript"> |
| 189 | (function($){ |
| 190 | |
| 191 | // vars |
| 192 | var $a = $('#acf-export-copy'); |
| 193 | var $textarea = $('#acf-export-textarea'); |
| 194 | |
| 195 | |
| 196 | // remove $a if 'copy' is not supported |
| 197 | if( !document.queryCommandSupported('copy') ) { |
| 198 | return $a.remove(); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // event |
| 203 | $a.on('click', function( e ){ |
| 204 | |
| 205 | // prevent default |
| 206 | e.preventDefault(); |
| 207 | |
| 208 | |
| 209 | // select |
| 210 | $textarea.get(0).select(); |
| 211 | |
| 212 | |
| 213 | // try |
| 214 | try { |
| 215 | |
| 216 | // copy |
| 217 | var copy = document.execCommand('copy'); |
| 218 | if( !copy ) return; |
| 219 | |
| 220 | |
| 221 | // tooltip |
| 222 | acf.newTooltip({ |
| 223 | text: "<?php _e('Copied', 'acf' ); ?>", |
| 224 | timeout: 250, |
| 225 | target: $(this), |
| 226 | }); |
| 227 | |
| 228 | } catch (err) { |
| 229 | |
| 230 | // do nothing |
| 231 | |
| 232 | } |
| 233 | |
| 234 | }); |
| 235 | |
| 236 | })(jQuery); |
| 237 | </script> |
| 238 | </div> |
| 239 | </div> |
| 240 | <?php |
| 241 | |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | function get_selected(){ |
| 247 | |
| 248 | // vars |
| 249 | $selected = $this->get_selected_keys(); |
| 250 | |
| 251 | if(!$selected) |
| 252 | return false; |
| 253 | |
| 254 | // Return |
| 255 | $array = array(); |
| 256 | |
| 257 | // Enable filters |
| 258 | acf_enable_filters(); |
| 259 | |
| 260 | foreach($selected as $field_group_key){ |
| 261 | |
| 262 | $field_group = acf_get_field_group($field_group_key); |
| 263 | |
| 264 | // validate field group |
| 265 | if(empty($field_group)) |
| 266 | continue; |
| 267 | |
| 268 | // load fields |
| 269 | $field_group['fields'] = acf_get_fields($field_group); |
| 270 | |
| 271 | // prepare for export |
| 272 | $field_group = acf_prepare_field_group_for_export($field_group); |
| 273 | |
| 274 | $array[] = $field_group; |
| 275 | |
| 276 | } |
| 277 | |
| 278 | // return |
| 279 | return $array; |
| 280 | |
| 281 | } |
| 282 | |
| 283 | function get_selected_keys(){ |
| 284 | |
| 285 | // check $_POST |
| 286 | if($keys = acf_maybe_get_POST('keys')) |
| 287 | return (array) $keys; |
| 288 | |
| 289 | // check $_GET |
| 290 | if($keys = acf_maybe_get_GET('keys')){ |
| 291 | |
| 292 | $keys = str_replace(' ', '+', $keys); |
| 293 | return explode('+', $keys); |
| 294 | |
| 295 | } |
| 296 | |
| 297 | // return |
| 298 | return false; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | function get_action(){ |
| 303 | |
| 304 | // check $_POST |
| 305 | if($action = acf_maybe_get_POST('action')) |
| 306 | return $action; |
| 307 | |
| 308 | // check $_GET |
| 309 | if($action = acf_maybe_get_GET('action')) |
| 310 | return $action; |
| 311 | |
| 312 | // return |
| 313 | return 'json'; |
| 314 | |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | |
| 319 | acf_register_admin_tool('ACFE_Admin_Tool_FG_Local'); |
| 320 | |
| 321 | endif; |