Hi @imokweb,
Hope I’m not too late
Here is the class I’m using to “pluginize” the CPT UI data (as json) on one hand and that load the JSON data on the other hand.
I did not manage to explore all of the of the recomandations given by @tw2113 in this answer (thanks by the way) so things can surely be improved. However it works for me as it is ??
@tw2113 Also thanks for adding the issue in consideration !
<?php
/**
* Class description
*
* CPTUI_Sync has two mains roles :
* 1- Pluginize CPT UI data, which mean exporting the data as a JSON file.
* When pluginizing is on, the data are pluginized whenever CPType or CPTax creation / update /deletion is detected.
*
* 2- Register / load pluginized data
* CPTUI_Sync will automatically detect the existence of pluginized data and will automatically register them.
*
* How does it work :
*
* Instanciate the class will automatically register pluginized data if such data exists
* >> <code>$cpt_ui_synchronizer = new CPTUI_Sync();</code>
*
* Pluginizing the data is something one may want to do only in specific use case (e.g : on your local dev environment)
* So, one will have to implement one extra step to activate the plugination, depending on your logic / orgainization
* >> <code>$cpt_ui_synchronizer->pluginized_data();</code>
*
*
* Support : https://www.ads-software.com/support/topic/exporting-cpt-ui-data-to-file-for-source-control-and-registering-data-from-files/
*/
class CPTUI_Sync {
const CONFIG_DIR_NAME = 'cptui-json';
const CUSTOM_POST_TYPE_FILENAME = 'cptui-post-types-config.json';
const CUSTOM_POST_TAX_FILENAME = 'cptui-post-tax-config.json';
function __construct() {
add_action( 'init', [ $this, "register_post_type" ] );
add_action( 'init', [ $this, "register_tax" ] );
}
/**
* Use the CPT UI hooks to detect CPType & CPTax registration / update or deletion to pluginize the data
*/
public function pluginized_data() {
// Initialize the folder that will receive pluginized data
self::initialize_config_dir();
// After CPT UI Register / update / delete post types
add_action( 'cptui_post_register_post_types', [ $this, "pluginize_local_cptui_post_types" ] );
add_action( 'cptui_after_update_post_type' , [ $this, "pluginize_local_cptui_post_types" ] );
add_action( 'cptui_after_delete_post_type' , [ $this, "pluginize_local_cptui_post_types" ] );
// After CPT UI Register / update / delete taxonomies
add_action( 'cptui_post_register_taxonomies', [ $this, "pluginize_local_cptui_taxonomies" ] );
add_action( 'cptui_after_update_taxonomy' , [ $this, "pluginize_local_cptui_taxonomies" ] );
add_action( 'cptui_after_delete_taxonomy' , [ $this, "pluginize_local_cptui_taxonomies" ] );
}
/**
* Register taxonomies from config file
* Bug : when json decoding the data it seems that bool value are considered as string
*/
public static function register_tax() {
$config_file = self::get_cptui_post_tax_config_file();
if( file_exists( $config_file ) ) {
$json_config = file_get_contents($config_file);
$taxonomies = json_decode($json_config);
if( !empty($taxonomies) ){
foreach($taxonomies as $taxonomy) {
if ( function_exists("cptui_register_single_taxonomy") ) {
cptui_register_single_taxonomy( (array) $taxonomy );
}
}
}
}
}
/**
* Register post type from config file
*/
public static function register_post_type() {
$config_file = self::get_cptui_post_type_config_file();
if( file_exists( $config_file ) ) {
$json_config = file_get_contents($config_file);
$post_types = json_decode($json_config);
if( !empty($post_types) ){
foreach($post_types as $post_type) {
if ( function_exists("cptui_register_single_post_type") ) {
cptui_register_single_post_type( (array) $post_type );
}
}
}
}
}
/**
* Retrieve the cptui config folder path
*/
public static function get_config_dir() {
$theme_dir = get_stylesheet_directory();
return $theme_dir."/".self::CONFIG_DIR_NAME;
}
/**
* Retrieve the post type config file path
*/
public static function get_cptui_post_type_config_file() {
return self::get_config_dir()."/".self::CUSTOM_POST_TYPE_FILENAME;
}
/**
* Retrieve the post tax config file path
*/
public static function get_cptui_post_tax_config_file() {
return self::get_config_dir()."/".self::CUSTOM_POST_TAX_FILENAME;
}
/**
* Create config dir
*/
private function initialize_config_dir(){
$cptui_json_dir = self::get_config_dir();
// Create the config directory if it doesn't exist.
if ( ! is_dir( $cptui_json_dir ) ) {
mkdir( $cptui_json_dir, 0755 );
}
}
public static function pluginize_local_cptui_post_types( $data = [] ) {
// Fetch all of our post types and encode into JSON.
$cptui_post_types = get_option( 'cptui_post_types', [] );
array_walk_recursive($cptui_post_types, 'self::set_disp_value' );
$content = json_encode( $cptui_post_types );
// Save the encoded JSON to a JSON file holding all of them.
$cptui_json_dir = self::get_config_dir();
file_put_contents( $cptui_json_dir . '/cptui-post-types-config.json', $content );
}
public static function pluginize_local_cptui_taxonomies( $data = [] ) {
// Fetch all of our taxonomies and encode into JSON.
$cptui_taxonomies = get_option( 'cptui_taxonomies', [] );
array_walk_recursive($cptui_taxonomies, 'self::set_disp_value' );
$content = json_encode( $cptui_taxonomies );
// Save the encoded JSON to a JSON file holding all of them.
$cptui_json_dir = self::get_config_dir();
file_put_contents( $cptui_json_dir . '/cptui-post-tax-config.json', $content );
}
private static function set_disp_value(&$value)
{
if ( 'false' === $value ) {
$value = false;
}
elseif ( 'true' === $value ) {
$value = true;
}
}
}