diff --git a/.gitignore b/.gitignore index 090a1f0..a75c826 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea .DS_Store +fewbricks-demo/gitignored/* diff --git a/admin/views/dev.view.php b/admin/views/dev.view.php index c2e718d..c512f0c 100644 --- a/admin/views/dev.view.php +++ b/admin/views/dev.view.php @@ -37,7 +37,7 @@ foreach ($fieldGroupCodes AS $fieldGroupKey => $data) { - $textarea_content .= $data[1]; + $textarea_content .= esc_textarea($data[1]); } @@ -62,6 +62,7 @@
diff --git a/fewbricks-demo/demo-functions.php b/fewbricks-demo/demo-functions.php index 951ca81..15b47b1 100644 --- a/fewbricks-demo/demo-functions.php +++ b/fewbricks-demo/demo-functions.php @@ -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(); + +} + + diff --git a/fewbricks-demo/lib/Filters.php b/fewbricks-demo/lib/Filters.php index 3aff455..637303f 100644 --- a/fewbricks-demo/lib/Filters.php +++ b/fewbricks-demo/lib/Filters.php @@ -3,6 +3,7 @@ /** * Demoing available filters */ + namespace App\FewbricksDemo; /** @@ -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() @@ -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'; } @@ -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'; + + } + } diff --git a/src/Admin.php b/src/Admin.php index 32f8415..398b1d7 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -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'); } @@ -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(); - - } - - } - } diff --git a/src/Fewbricks.php b/src/Fewbricks.php index 793236b..ee6bf39 100644 --- a/src/Fewbricks.php +++ b/src/Fewbricks.php @@ -29,6 +29,18 @@ public static function run() Admin::applyHooks(); } + self::applyHooks(); + + } + + /** + * + */ + public static function applyHooks() + { + + add_action('wp_loaded', __NAMESPACE__ . '\\Helper::maybeWriteToPhpCodeFile'); + } /** diff --git a/src/Helper.php b/src/Helper.php index 4a9baa0..ce44d04 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -49,87 +49,6 @@ public static function fewbricksIsInDebugMode() } - /** - * @param mixed|boolean $fieldGroupKeys An array with the keys of the field groups you want to get. Pass false to - * retrieve all stored field groups. - * - * @return array - */ - public static function getFieldGroupsPhpCodes($fieldGroupKeys = false) - { - - $codes = []; - - $storedSettings = self::getStoredFieldGroupsAcfSettings(); - - // Taken from class-acf-admin-tool-export.php - $str_replace = [ - " " => "\t", - "'!!__(!!\'" => "__('", - "!!\', !!\'" => "', '", - "!!\')!!'" => "')", - "array (" => "array(", - ]; - $preg_replace = [ - '/([\t\r\n]+?)array/' => 'array', - '/[0-9]+ => array/' => 'array', - ]; - - // Loop the keys the caller has requested - foreach ($fieldGroupKeys AS $fieldGroupKey) { - - if (isset($storedSettings[$fieldGroupKey])) { - - $settingsCode = var_export($storedSettings[$fieldGroupKey], true); - - // From ACF - $settingsCode = str_replace(array_keys($str_replace), array_values($str_replace), $settingsCode); - - // From ACF - $settingsCode = preg_replace(array_keys($preg_replace), array_values($preg_replace), $settingsCode); - - $settingsCode = esc_textarea($settingsCode); - - $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 any code for this field group.', 'fewbricks'); - - } - - $codes[$fieldGroupKey] = [$storedSettings[$fieldGroupKey]['title'], $code]; - - } - - return $codes; - - } - - /** - * @return mixed - */ - public static function getStoredFieldGroupsAcfSettings() - { - - global $fieldGroupsAcfSettings; - - return $fieldGroupsAcfSettings; - - } - /** * Returns a timestamp if we are in dev environment. Use for example when developing css and js. * @@ -271,10 +190,13 @@ public static function maybeStoreFieldGroupAcfSettings($fieldGroupAcfSettings) { if ( - (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']) + ( + (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']) + ) + || self::getAutoWritePhpCodeFile() !== false ) { self::storeFieldGroupAcfSettings($fieldGroupAcfSettings); @@ -292,6 +214,8 @@ public static function generatePhpCodeTriggered() return self::pageIsFewbricksAdminPage() && isset($_GET['action']) && $_GET['action'] === 'fewbricks_generate_php' + && isset($_GET['fewbricks_selected_field_groups_for_export']) + && is_array($_GET['fewbricks_selected_field_groups_for_export']) && wp_verify_nonce($_GET['_wpnonce'], 'fewbricks_export_d89dtygodl'); } @@ -309,6 +233,16 @@ public static function exportToJsonTriggered() } + /** + * @return string|boolean False if no file should be used. + */ + public static function getAutoWritePhpCodeFile() + { + + return apply_filters('fewbricks/auto_write_php_code_file', false); + + } + /** * @param array $fieldGroupAcfSettings */ @@ -317,7 +251,7 @@ public static function storeFieldGroupAcfSettings($fieldGroupAcfSettings) global $fieldGroupsAcfSettings; - if(!is_array($fieldGroupsAcfSettings)) { + if (!is_array($fieldGroupsAcfSettings)) { $fieldGroupsAcfSettings = []; } @@ -348,6 +282,57 @@ public static function pageIsFewbricksAdminPage() } + /** + * + */ + public static function maybeExportJson() + { + + $data = []; + + if (Helper::exportToJsonTriggered()) { + + $fieldGroups = Helper::getStoredFieldGroupsAcfSettings(); + + if (!empty($fieldGroups)) { + + // 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(); + + } + + } + + } + + /** + * @return mixed + */ + public static function getStoredFieldGroupsAcfSettings() + { + + global $fieldGroupsAcfSettings; + + return $fieldGroupsAcfSettings; + + } + /** * @param $fieldGroupTitle * @param $fieldGroupId @@ -359,7 +344,7 @@ public static function maybeStoreSimpleFieldGroupData($fieldGroupTitle, $fieldGr global $simpleFieldGroupsData; - if(!is_array($simpleFieldGroupsData)) { + if (!is_array($simpleFieldGroupsData)) { $simpleFieldGroupsData = []; } @@ -369,6 +354,139 @@ public static function maybeStoreSimpleFieldGroupData($fieldGroupTitle, $fieldGr } + /** + * + */ + public static function maybeWriteToPhpCodeFile() + { + + $codeToWrite = ''; + + $targetFile = self::getAutoWritePhpCodeFile(); + + if ($targetFile !== false) { + + $codes = self::getFieldGroupsPhpCodes(); + + if(is_array($codes)) { + + foreach($codes AS $code) { + + $codeToWrite .= $code[1]; + + } + + } + + if(!empty($codeToWrite)) { + + file_put_contents($targetFile, "'; + $message .= '' . sprintf(__('PHP code written to %s
', 'fewbricks'), Helper::getAutoWritePhpCodeFile()) . '