Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active June 7, 2024 18:35
Show Gist options
  • Save FrancoStino/81e1eb126bf8f24b7a43643b9379ac87 to your computer and use it in GitHub Desktop.
Save FrancoStino/81e1eb126bf8f24b7a43643b9379ac87 to your computer and use it in GitHub Desktop.
Check if WP_CLI is defined and if the ACF_Commands class does not already exist, then synchronize ACF fields.

Sync ACF Fields on WP_CLI Commands

Preview:
// Controlla se WP_CLI è definito e se non esiste già la classe ACF_Commands
if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'ACF_Commands' ) ) :

    /**
     * Classe ACF_Commands
     */
    class ACF_Commands extends WP_CLI_Command {

        /**
         * Sincronizza i campi ACF
         *
         * ## OPZIONI
         *
         * @when init
         *
         * @esempio
         *
         *  wp acf sync
         *
         */
        function sync ( $args, $assoc_args ) {

            // Variabili
            $groups = acf_get_field_groups();
            $sync 	= array();

            // Interrompe l'esecuzione se non ci sono gruppi di campi
            if( empty( $groups ) )
                return;

            // Trova i gruppi di campi JSON che non sono ancora stati importati
            foreach( $groups as $group ) {

                // Variabili
                $local 		= acf_maybe_get( $group, 'local', false );
                $modified 	= acf_maybe_get( $group, 'modified', 0 );
                $private 	= acf_maybe_get( $group, 'private', false );

                // Ignora i gruppi di campi nel database, PHP o privati
                if( $local !== 'json' || $private ) {

                    // Non fare nulla

                } elseif( ! $group[ 'ID' ] ) {

                    $sync[ $group[ 'key' ] ] = $group;

                } elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {

                    $sync[ $group[ 'key' ] ]  = $group;
                }
            }

            // Interrompe se non è necessaria alcuna sincronizzazione
            if( empty( $sync ) ) {
                WP_CLI::success( "Nessuna sincronizzazione ACF richiesta" );
                return;
            }

            if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {

                // Variabili
                $new_ids = array();

                foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {

                    // Aggiungi campi
                    if( acf_have_local_fields( $key ) ) {

                        $sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );

                    }
                    // Importa
                    $field_group = acf_import_field_group( $sync[ $key ] );
                }
            }

            WP_CLI::success( 'Sincronizzazione ACF riuscita!' );
        }

    }

    // Aggiunge il comando 'acf' a WP-CLI
    WP_CLI::add_command( 'acf', 'ACF_Commands' );

endif; // Fine definizione classe ACF_Commands
Associated Context
Type Code Snippet ( .php )
Associated Tags WP_CLI ACF_Commands Sincronizza i campi ACF OPZIONI WP acf sync JSON data type PHP o privati get_post_modified_time function ID property Framework: WP-CLI synchronization WordPress PHP
💡 Smart Description This code snippet defines a class called ACF_Commands that extends the WP_CLI. It checks if there are any non-esiste già la classe ACF_Commands classes, and if so, it creates an array of unique group keys based on their local/modified values in each group
Check if WP_CLI is defined and if the ACF_Commands class does not already exist, then synchronize ACF fields.
🔎 Suggested Searches WP_CLI sync function with multiple fields
Related Links https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
https://www.advancedcustomfields.com/resources/
https://developer.wordpress.org/cli/commands/
Related People Davide Ladisa
Sensitive Information No Sensitive Information Detected
Shareable Link https://davideladisa.pieces.cloud/?p=655e4ea79a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment