Skip to content

Commit

Permalink
WIP export functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
folbert committed Dec 22, 2017
1 parent 44356b8 commit 87986ca
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 60 deletions.
79 changes: 46 additions & 33 deletions admin/views/dev.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
<h1>Fewbricks</h1>

<?php
if (
isset($_GET['fewbricks_generate_php'])
&& isset($_GET['fewbricks_field_to_php'])
&& !empty($_GET['fewbricks_field_to_php'])
&& wp_verify_nonce($_GET['_wpnonce'], 'fewbricks_generate_php_rg8392god')
) {
if (\Fewbricks\Helper::generatePhpCodeTriggered()) {

$fieldGroupCodes = \Fewbricks\Helper::getFieldGroupsPhpCodes($_GET['fewbricks_field_to_php']);
$fieldGroupCodes
= \Fewbricks\Helper::getFieldGroupsPhpCodes($_GET['fewbricks_selected_field_groups_for_export']);

if (!empty($fieldGroupCodes)) {
?>

<div class="acf-meta-box-wrap">
<div class="postbox">

<h2 class="hndle"><?php _e('Export Field Groups', 'fewbricks'); ?></h2>
<h2 class="hndle"><?php _e('Export Field Groups', 'acf'); ?></h2>

<div class="inside">

Expand All @@ -41,16 +37,8 @@

foreach ($fieldGroupCodes AS $fieldGroupKey => $data) {

$textarea_content .= "//-------------\r";
$textarea_content .= "// Start of field group \"" . $data[0] . "\"\r";
$textarea_content .= "//-------------\r\r";

$textarea_content .= $data[1];

$textarea_content .= "\r//-------------\r";
$textarea_content .= "// End of field group \"" . $data[0] . "\"\r";
$textarea_content .= "//-------------\r\r";

}

echo '<textarea readonly class="fewbricks__export-textarea">' . $textarea_content
Expand All @@ -76,11 +64,13 @@
<div class="acf-meta-box-wrap -grid">
<div class="postbox">

<h2 class="hndle">Export Fewbricks field groups and fields</h2>
<h2 class="hndle"><?php _e('Export Fewbricks field groups and fields', 'fewbricks'); ?></h2>

<div class="inside">

<p>Here you can export all the field groups and fields that are registered using Fewbricks</p>
<p><?php _e('Here you can export all the field groups and fields that are registered using
Fewbricks', 'fewbricks'); ?></p>

<form action="<?php echo admin_url('edit.php'); ?>" method="get">

<div class="acf-fields">
Expand All @@ -101,22 +91,23 @@
$checkboxesHtml .= '<li><label><input type="checkbox" class="acf-checkbox-toggle" data-fewbricks-toggle-all-siblings><i> '
. __('Toggle all', 'fewbricks') . '</i></li>';

foreach ($fieldGroupData AS $fieldGroupKey => $fieldGroupKey) {
foreach ($fieldGroupData AS $fieldGroupKey => $fieldGroupTitle) {

$checkboxesHtml .= '<li>';
$checkboxesHtml .= '<label>';
$checkboxesHtml .= '<input type="checkbox" ';
$checkboxesHtml .= 'name="fewbricks_field_to_php[]" ';
$checkboxesHtml .= 'name="fewbricks_selected_field_groups_for_export[]" ';
$checkboxesHtml .= 'value="' . $fieldGroupKey . '" ';

if (isset($_GET['fewbricks_field_to_php'])
&& in_array($fieldGroupKey, $_GET['fewbricks_field_to_php'])
if (isset($_GET['fewbricks_selected_field_groups_for_export'])
&& in_array($fieldGroupKey,
$_GET['fewbricks_selected_field_groups_for_export'])
) {
$checkboxesHtml .= 'checked="checked" ';
}

$checkboxesHtml .= '> ';
$checkboxesHtml .= $fieldGroupKey;
$checkboxesHtml .= $fieldGroupTitle;
$checkboxesHtml .= '</label>';
$checkboxesHtml .= '</li>';

Expand All @@ -131,16 +122,26 @@
<hr>

<div class="acf-label">
<label for="fewbricks_generate_php_split">Generate:</label>
<label for="fewbricks_generate_php_split"><?php _e('Generate (only used when
generating PHP):', 'fewbricks'); ?></label>
</div>
<select name="fewbricks_generate_php_split" id="fewbricks_generate_php_split">
<option value="one_per_field_group">One textearea per field group</option>
<option value="no_split">One textarea with all field groups in it</option>
<option value="one_per_field_group"><?php _e('One textarea per field
group', 'fewbricks'); ?></option>
<option value="no_split"><?php _e('One textarea with all field groups in
it', 'fewbricks'); ?></option>
</select>

<?php submit_button('Generate PHP', 'primary', 'fewbricks_generate_php'); ?>
<?php wp_nonce_field('fewbricks_generate_php_rg8392god', '_wpnonce', false); ?>
<input type="hidden" name="action" value="fewbricks_generate_php" value="1"/>
<p class="acf-submit">
<button type="submit" name="action" class="button button-primary"
value="fewbricks_export_json"><?php _e('Export JSON-file',
'fewbricks'); ?></button>
<button type="submit" name="action" class="button"
value="fewbricks_generate_php"><?php _e('Generate PHP',
'acf'); ?></button>
</p>

<?php wp_nonce_field('fewbricks_export_d89dtygodl', '_wpnonce', false); ?>
<input type="hidden" name="post_type" value="acf-field-group"/>
<input type="hidden" name="page" value="fewbricksdev"/>

Expand All @@ -149,7 +150,9 @@
} else {
?>

There are no fields registered by Fewbricks.
<p>
<?php _e('There are no fields registered by Fewbricks.', 'fewbricks'); ?>
</p>

<?php

Expand All @@ -169,8 +172,18 @@

</div>

</div>
<div class="acf-meta-box-wrap -grid">
<div class="postbox">

<h2 class="hndle">Export Fewbricks field groups and fields</h2>

<div class="inside">

<?php
dhjkhdkjhdkjhkjhjk

</div>

\Fewbricks\Helper::cleanUpAfterAdminPage();
</div>
</div>

</div>
36 changes: 36 additions & 0 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static function applyHooks()

add_action('admin_enqueue_scripts', __NAMESPACE__ . '\\Admin::applyStyles');
add_action('admin_menu', __NAMESPACE__ . '\\Admin::editMenu');
add_action('admin_menu', __NAMESPACE__ . '\\Admin::maybeExportJson');

}

Expand Down Expand Up @@ -56,4 +57,39 @@ function () {

}

/**
*
*/
public static function maybeExportJson()
{

$data = [];

if (Helper::exportToJsonTriggered()) {

$fieldGroups = Helper::getStoredFieldGroupsAcfSettings();

// construct JSON
foreach ($fieldGroups as $fieldGroup) {

// prepare for export
$fieldGroup = acf_prepare_field_group_for_export($fieldGroup);

// add to json array
$data[] = $fieldGroup;

}

$file_name = 'fewbricks-acf-export-' . date('Y-m-d') . '.json';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename={$file_name}");
header("Content-Type: application/json; charset=utf-8");

echo acf_json_encode($data);
die();

}

}

}
94 changes: 67 additions & 27 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ public static function acfIsActivated()

}

/**
*
*/
public static function cleanUpAfterAdminPage()
{

delete_transient('fewbricks_field_groups_simple_data');

}

/**
* @return bool
*/
Expand Down Expand Up @@ -100,15 +90,23 @@ public static function getFieldGroupsPhpCodes($fieldGroupKeys = false)

$settingsCode = esc_textarea($settingsCode);

$code = "if( function_exists('acf_add_local_field_group') ) {\r\n";
$code = "//-------------\r";
$code .= "// Start of field group \"" . $storedSettings[$fieldGroupKey]['title'] . "\"\r";
$code .= "//-------------\r\r";

$code .= "if( function_exists('acf_add_local_field_group') ) {\r\n";
$code .= " acf_add_local_field_group(\r\n";
$code .= "\t" . $settingsCode;
$code .= " )\r\n";
$code .= '}';

$code .= "\r//-------------\r";
$code .= "// End of field group \"" . $storedSettings[$fieldGroupKey]['title'] . "\"\r";
$code .= "//-------------\r\r";

} else {

$code = 'Could not find the code for this field group.';
$code = __('Could not find any code for this field group.', 'fewbricks');

}

Expand All @@ -126,7 +124,9 @@ public static function getFieldGroupsPhpCodes($fieldGroupKeys = false)
public static function getStoredFieldGroupsAcfSettings()
{

return get_transient('fewbricks_field_groups_acf_settings');
global $fieldGroupsAcfSettings;

return $fieldGroupsAcfSettings;

}

Expand All @@ -146,7 +146,6 @@ public static function getVersionOrTimestamp()

return $outcome;


}

/**
Expand Down Expand Up @@ -272,19 +271,58 @@ public static function maybeStoreFieldGroupAcfSettings($fieldGroupAcfSettings)
{

if (
self::pageIsFewbricksAdminPage()
&& isset($_GET['fewbricks_field_to_php'])
&& in_array($fieldGroupAcfSettings['key'], $_GET['fewbricks_field_to_php'])
(self::generatePhpCodeTriggered() || self::exportToJsonTriggered())
&& isset($_GET['fewbricks_selected_field_groups_for_export'])
&& is_array($_GET['fewbricks_selected_field_groups_for_export'])
&& in_array($fieldGroupAcfSettings['key'], $_GET['fewbricks_selected_field_groups_for_export'])
) {

$settings = self::getStoredFieldGroupsAcfSettings();
self::storeFieldGroupAcfSettings($fieldGroupAcfSettings);

$settings[$fieldGroupAcfSettings['key']] = $fieldGroupAcfSettings;
}

}

set_transient('fewbricks_field_groups_acf_settings', $settings);
/**
* @return bool
*/
public static function generatePhpCodeTriggered()
{

return self::pageIsFewbricksAdminPage()
&& isset($_GET['action'])
&& $_GET['action'] === 'fewbricks_generate_php'
&& wp_verify_nonce($_GET['_wpnonce'], 'fewbricks_export_d89dtygodl');

}

/**
* @return bool
*/
public static function exportToJsonTriggered()
{

return self::pageIsFewbricksAdminPage()
&& isset($_GET['action'])
&& $_GET['action'] === 'fewbricks_export_json'
&& wp_verify_nonce($_GET['_wpnonce'], 'fewbricks_export_d89dtygodl');

}

/**
* @param array $fieldGroupAcfSettings
*/
public static function storeFieldGroupAcfSettings($fieldGroupAcfSettings)
{

global $fieldGroupsAcfSettings;

if(!is_array($fieldGroupsAcfSettings)) {
$fieldGroupsAcfSettings = [];
}

$fieldGroupsAcfSettings[$fieldGroupAcfSettings['key']] = $fieldGroupAcfSettings;

}

/**
Expand Down Expand Up @@ -319,11 +357,13 @@ public static function maybeStoreSimpleFieldGroupData($fieldGroupTitle, $fieldGr

if (self::pageIsFewbricksAdminPage()) {

$fieldGroupsData = self::getStoredSimpleFieldGroupData();
global $simpleFieldGroupsData;

$fieldGroupsData[$fieldGroupId] = $fieldGroupTitle;
if(!is_array($simpleFieldGroupsData)) {
$simpleFieldGroupsData = [];
}

set_transient('fewbricks_field_groups_simple_data', $fieldGroupsData);
$simpleFieldGroupsData[$fieldGroupId] = $fieldGroupTitle;

}

Expand All @@ -335,13 +375,13 @@ public static function maybeStoreSimpleFieldGroupData($fieldGroupTitle, $fieldGr
public static function getStoredSimpleFieldGroupData()
{

$data = get_transient('fewbricks_field_groups_simple_data');
global $simpleFieldGroupsData;

if ($data === false) {
$data = [];
if (!is_array($simpleFieldGroupsData)) {
$simpleFieldGroupsData = [];
}

return $data;
return $simpleFieldGroupsData;

}

Expand Down

0 comments on commit 87986ca

Please sign in to comment.