Skip to content

Commit

Permalink
Work on auto generate PHP file
Browse files Browse the repository at this point in the history
Also validated JSON export file by testing importing it to another
WP install.
  • Loading branch information
folbert committed Dec 25, 2017
1 parent 87986ca commit 4beef2f
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 175 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.idea
.DS_Store
fewbricks-demo/gitignored/*
18 changes: 9 additions & 9 deletions admin/views/dev.view.php
Expand Up @@ -37,7 +37,7 @@

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

$textarea_content .= $data[1];
$textarea_content .= esc_textarea($data[1]);

}

Expand All @@ -62,14 +62,15 @@


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

<div class="postbox">

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

<div class="inside">

<p><?php _e('Here you can export all the field groups and fields that are registered using
Fewbricks', 'fewbricks'); ?></p>
Fewbricks. Use the download button to export to a .json file which you can then import to another ACF installation. Use the generate button to export to PHP code which you can place in your theme.', 'fewbricks'); ?></p>

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

Expand Down Expand Up @@ -122,7 +123,7 @@
<hr>

<div class="acf-label">
<label for="fewbricks_generate_php_split"><?php _e('Generate (only used when
<label for="fewbricks_generate_php_split"><?php _e('Generate (only applied when
generating PHP):', 'fewbricks'); ?></label>
</div>
<select name="fewbricks_generate_php_split" id="fewbricks_generate_php_split">
Expand Down Expand Up @@ -170,20 +171,19 @@

</div>

</div>

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

<h2 class="hndle">Export Fewbricks field groups and fields</h2>
<h2 class="hndle">Automatically create and use PHP code</h2>

<div class="inside">

dhjkhdkjhdkjhkjhjk

Use the filter <a href="https://fewbricksdocs.readme.io/v2.0/docs/auto_write_php_code_file"
target="_blank">auto_write_php_code_file</a> to automatically render PHP code and
write it to a file on each page load.
</div>

</div>

</div>

</div>
96 changes: 54 additions & 42 deletions fewbricks-demo/demo-functions.php
Expand Up @@ -19,50 +19,62 @@
// Fewbricks as possible.
require_once 'demo-setup.php';

// Define the filters for the demo
Filters::defineHooks();

// Demoing a class
(new FieldsKitchenSink('1712042021a'))
->setTitle('Main content')
// This will be prefixed to all field names i the field group.
// Since we are re-using the same field group multiple times on the same page,
// this is necessary to avoid clashes
->setFieldNamesPrefix('main_content_')
->register();
// Demoing how to use "caching" functionality using PHP code files
if (Helper::getAutoWritePhpCodeFile() !== false) {

// Demoing the same class but but changing it on the fly
(new FieldsKitchenSink('17120529561a'))
->clearLocationRuleGroups()
->addLocationRuleGroups([
(new FieldGroupLocationRuleGroup())->addRule(
new Rule('post_type', '=', 'fewbricks_demo_pg2')
),
(new FieldGroupLocationRuleGroup())->addRule(
new Rule('post_type', '=', 'fewbricks_demo_pg')
),
])
->setTitle('Secondary content')
->hideOnScreen('the_content')
->setFieldNamesPrefix('secondary_content_')
->setFieldLabelsPrefix('Secondary content - ')
->register();
require_once Helper::getAutoWritePhpCodeFile();

(new FieldsKitchenSink('1712111413a'))
->removeField('fd_tab1')
->removeFields(['fd_color_picker', 'fd_file'])
->removeField('fd_checkbox')
->unRemoveField('fd_tab1')
->addFieldAfter(
new Text('Text - added after Button Group', 'fd_text_after_button_group', '1712121051a'),
'fd_button_group'
)
->addFieldAfter(
(new Text('Another text added after the button group', 'fd_text_after_new_text', '17121206a'))
->addConditionalLogicRuleGroup(
(new ConditionalLogicRuleGroup())
->addRule(new ConditionalLogicRule('17120529561a', '==', 'black')
)
} else {

// Demoing a class
(new FieldsKitchenSink('1712042021a'))
->setTitle('Main content')
// This will be prefixed to all field names i the field group.
// Since we are re-using the same field group multiple times on the same page,
// this is necessary to avoid clashes
->setFieldNamesPrefix('main_content_')
->register();

// Demoing the same class but but changing it on the fly
(new FieldsKitchenSink('17120529561a'))
->clearLocationRuleGroups()
->addLocationRuleGroups([
(new FieldGroupLocationRuleGroup())->addRule(
new Rule('post_type', '=', 'fewbricks_demo_pg2')
),
'fd_button_group'
)
->register();
(new FieldGroupLocationRuleGroup())->addRule(
new Rule('post_type', '=', 'fewbricks_demo_pg')
),
])
->setTitle('Secondary content')
->hideOnScreen('the_content')
->setFieldNamesPrefix('secondary_content_')
->setFieldLabelsPrefix('Secondary content - ')
->register();

(new FieldsKitchenSink('1712111413a'))
->removeField('fd_tab1')
->removeFields(['fd_color_picker', 'fd_file'])
->removeField('fd_checkbox')
->unRemoveField('fd_tab1')
->addFieldAfter(
new Text('Text - added after Button Group', 'fd_text_after_button_group', '1712121051a'),
'fd_button_group'
)
->addFieldAfter(
(new Text('Another text added after the button group', 'fd_text_after_new_text', '17121206a'))
->addConditionalLogicRuleGroup(
(new ConditionalLogicRuleGroup())
->addRule(new ConditionalLogicRule('17120529561a', '==', 'black')
)
),
'fd_button_group'
)
->register();

}


18 changes: 17 additions & 1 deletion fewbricks-demo/lib/Filters.php
Expand Up @@ -3,6 +3,7 @@
/**
* Demoing available filters
*/

namespace App\FewbricksDemo;

/**
Expand Down Expand Up @@ -38,16 +39,21 @@ public static function defineHooks()
add_action('admin_notices',
[$me, 'editFieldGroupInfo']);

add_filter('fewbricks/auto_write_php_code_file', [$me, 'setPhpCodeFilePath']);

add_filter('fewbricks/show_fields_info', '__return_true');

add_filter('fewbricks/debug_mode', '__return_true');

add_filter('fewbricks/activate_field_snitch', '__return_true');

//add_filter('fewbricks/display_php_file_written_message', '__return_false');

}

/**
* This does not affect anything since the function is not called. Only here to show it should be done.
*
* @return string
*/
/*public static function getBrickTemplatesBasePath()
Expand All @@ -74,7 +80,7 @@ public static function getProjectFilesBasePath()
{

//return get_template_directory() . '/fewbricks2';
return WP_PLUGIN_DIR . '/fewbricks/fewbricks';
return WP_PLUGIN_DIR . '/fewbricks/fewbricks-demo';

}

Expand Down Expand Up @@ -108,4 +114,14 @@ public static function editFieldGroupInfo()

}

/**
* @return string
*/
public static function setPhpCodeFilePath()
{

return WP_PLUGIN_DIR . '/fewbricks/fewbricks-demo/gitignored/fewbricks-php.php';

}

}
37 changes: 1 addition & 36 deletions src/Admin.php
Expand Up @@ -18,7 +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');
add_action('admin_menu', 'Fewbricks\\Helper::maybeExportJson');

}

Expand Down Expand Up @@ -57,39 +57,4 @@ 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();

}

}

}
12 changes: 12 additions & 0 deletions src/Fewbricks.php
Expand Up @@ -29,6 +29,18 @@ public static function run()
Admin::applyHooks();
}

self::applyHooks();

}

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

add_action('wp_loaded', __NAMESPACE__ . '\\Helper::maybeWriteToPhpCodeFile');

}

/**
Expand Down

0 comments on commit 4beef2f

Please sign in to comment.