• Resolved Tofferus

    (@claborier)


    Hi,

    Here is my situation :

    1. I need to export the CPT UI registered data in a file automatically whenever I create/update a post type or a taxonomie.
    2. Also, I want to be able to import the data stored in the generated files automatically

    1- Exporting the data

    I found this related topic leading to this interesting source of yours showing how to save data to a JSON file.

    So far so good, I took the code and adapt it to my need. So now I am able to exportthe CPT UI data to JSON files.

    But the code do not bring the second part : registering post type or taxonomies from the JSON files (I don’t want to have to import them with the Worpdress UI, it has to be programmatic)

    2- Importing the data

    So I created my own code, here is the one to register my post types from the JSON 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);
        
        foreach($post_types as $key => $post_type) {
          register_post_type( $key, $post_type );
        }
      }
    }

    The problem is when I decode the JSON files, the boolean values (like hierarchical) are interpreted as a string and not as a boolean. So it break some stuffs.

    For ex: the WP function is_post_type_hierarchical() will return a string and testing it like this : if(is_post_type_hierarchical('my_custom_post')) ... will return true… inconvenient…

    3- My questions ! (finally)

    You should have been facing the same issue with your “import JSON” tool. But I didn’t find out how you achieve this.

    Can you lead me on the right way ?

    Thanks

    • This topic was modified 2 years, 7 months ago by Tofferus.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Slightly a long function because of comments and other various cleanup work we do in there, but check out https://github.com/WebDevStudios/custom-post-type-ui/blob/1.11.2/custom-post-type-ui.php#L339-L543

    We don’t just dump the decoded JSON into the register_post_type() call.

    For example with the boolean values, we make use of these two functions:

    https://github.com/WebDevStudios/custom-post-type-ui/blob/9dd873d4bf2229b590db10567c9b7aa7effdd030/inc/utility.php#L63-L70

    https://github.com/WebDevStudios/custom-post-type-ui/blob/9dd873d4bf2229b590db10567c9b7aa7effdd030/inc/utility.php#L80-L87

    First one will return a true boolean value comparably to the value passed in, while the second will return a string equivalent.

    I’m foreseeing two possible next steps for you, and ones we should really be getting documented better for everyone to make use of.

    First one, would be utilizing cptui_register_single_post_type() where you use it in your foreach loop. Just pass in the individual index which should be keyed by post type slug and self contained in the index.

    Or

    Second one, make use of the filter outlined in https://github.com/WebDevStudios/custom-post-type-ui/blob/1.11.2/custom-post-type-ui.php#L256-L274

    I’m highlighting a bit more than just the filter to show relevant parts. You’d basically just be decoding the JSON version, and then returning it as the filter return value. This would override the database version with the saved file’s version, and save a little bit of work of trying to register it all twice. This would only have the initial options fetch as an extra query.

    I’d lean towards the 2nd one personally.

    Thread Starter Tofferus

    (@claborier)

    Hi,

    Sorry for the delay, I’m running out of time ??

    Anyway, many thanks for your answer ! To let you know, I just adapt my code to implement the first solution you showed me. So I make use of cptui_register_single_post_type & cptui_register_single_taxonomy and everything is going well ??

    However I think I would like to implement the second way as you advice me, but I will need some more time to study that, and I may have some more questions.

    Once I do that, I intend to share my code. But if someone need it right now, feel free to ask.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    You know where to find us.

    @claborier
    Hello,

    First of all, congrats on this question because this is really important when it comes to working in local dev environments with multiple team members.

    Could you please share the code you created for the import? We really need it. If you did not manage to do it, we will attempt to do it ourselves.

    @tw2113
    It would really come in handy if the core dev team could implement something like the ACF fields has, where this functionality is taken care of by the core plugin, rather than everybody writing their own code.
    Example: https://www.billerickson.net/acf-json-with-git/

    Ultimately, it would be even better if migrations finally could reach WordPress just like Laravel does it in its elegant way.
    https://laravel.com/docs/9.x/migrations

    Thanks for everything,
    Keep on rocking ??
    I.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I know I have https://github.com/WebDevStudios/custom-post-type-ui/issues/381#issuecomment-266074940 from years back, but I’m willing to revisit upon further reflection.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thread Starter Tofferus

    (@claborier)

    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;
        }
      }
    
      
    }
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Exporting CPT UI data to file for source control and registering data from files’ is closed to new replies.