Plugin Directory

Changeset 3218193


Ignore:
Timestamp:
01/07/2025 08:54:29 AM (15 months ago)
Author:
navzme
Message:

[Added] Migrate from ACF Gallery Field or ACF Gallery Pro to ACF Galerie 4.

Location:
acf-galerie-4/trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • acf-galerie-4/trunk/acf-galerie-4.php

    r3197857 r3218193  
    99 * License: GPL v2 or later
    1010 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    11  * Version: 1.3.1
     11 * Version: 1.3.2
    1212 * Domain Path: /lang
    1313 * Requires PHP: 7.4
     
    1919}
    2020
    21 define( 'ACFG4_VERSION', '1.3.1' );
     21define( 'ACFG4_VERSION', '1.3.2' );
    2222define( 'ACFG4_PLUGIN', __FILE__ );
    2323define( 'ACFG4_PLUGIN_BASENAME', plugin_basename( ACFG4_PLUGIN ) );
     
    8080    new acfg4_wpgraphql();
    8181});
     82
     83add_action('admin_head', 'acfg4_start_migration_nonce');
     84function acfg4_start_migration_nonce() {
     85    if ( !is_admin() ) return;
     86    $nonce = wp_create_nonce('acfg4_start_migration_nonce');
     87?>
     88    <script type="text/javascript">
     89        const acfg4_start_migration_nonce = "<?php echo $nonce; ?>";
     90    </script>
     91<?php
     92}
     93
     94add_action('admin_enqueue_scripts', 'enqueue_plugin_admin_scripts');
     95function enqueue_plugin_admin_scripts() {
     96    wp_enqueue_script(
     97        'acfg4-admin-script',
     98        plugin_dir_url(__FILE__) . 'assets/js/admin-script.js',
     99        ['jquery'],
     100        '1.0.0',
     101        true
     102    );
     103}
     104
     105add_action('admin_enqueue_scripts', 'enqueue_plugin_admin_styles');
     106function enqueue_plugin_admin_styles() {
     107    wp_enqueue_style(
     108        'acfg4-admin-css',
     109        plugin_dir_url(__FILE__) . 'assets/css/admin-style.css',
     110        [],
     111        '1.0.0'
     112    );
     113}
     114
     115add_action('wp_ajax_acfg4_start_migration', 'acfg4_start_migration');
     116function acfg4_start_migration() {
     117    global $wpdb;
     118    $wpdb->query('START TRANSACTION');
     119
     120    try {
     121        $migrate_from = $_POST['migrate_from'];
     122
     123        if (
     124            isset( $_POST['nonce'] ) &&
     125            !wp_verify_nonce( $_POST['nonce'], 'acfg4_start_migration_nonce') )
     126        {
     127            wp_send_json_error(['message' => "Nonce verification failed. Please try again."], 400);
     128        }
     129
     130        if( !in_array( $migrate_from, [1, 2] ) ){
     131            wp_send_json_error(['message' => "Choose which plugin you want to migrate from."], 400);
     132        }
     133
     134        $fields = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = 'acf-field'");
     135
     136        foreach( $fields as $field ){
     137            $field_name = $field->post_excerpt;
     138            $field_metadata = unserialize( $field->post_content );
     139            $field_type = $field_metadata['type'];
     140
     141            if( in_array( $field_type, ['photo_gallery', 'gallery'])){
     142                $field_metadata['type'] = 'galerie-4';
     143                $updated_content = serialize($field_metadata);
     144
     145                $wpdb->update(
     146                    $wpdb->posts,
     147                    array( 'post_content' => $updated_content ),
     148                    array( 'ID' => $field->ID )
     149                );
     150
     151                //If ACF Photo Gallery Field, we want the ID's to be serialized.
     152                if( $migrate_from == 1 ){
     153                    $meta_fields = $wpdb->get_results(
     154                        $wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s", $field_name )
     155                    );
     156
     157                    foreach( $meta_fields as $meta){
     158                        $meta_value = array_filter( explode(',', $meta->meta_value) );
     159                        $meta_value_serialized = serialize( $meta_value );
     160   
     161                        $wpdb->update(
     162                            $wpdb->postmeta,
     163                            array( 'meta_value' => $meta_value_serialized ),
     164                            array( 'meta_id' => $meta->meta_id )
     165                        );
     166                    }
     167                }
     168            }
     169        }
     170
     171        $wpdb->query('COMMIT');
     172
     173        wp_send_json_success([
     174            'message' => 'Migration has successfully completed.'
     175        ]);
     176    } catch (Exception $e) {
     177        $wpdb->query('ROLLBACK');
     178        wp_send_json_error(['message' => $e->getMessage()], 500);
     179    }
     180
     181    die();
     182}
  • acf-galerie-4/trunk/class-acfg4-register-field-type.php

    r3197857 r3218193  
    8888       
    8989        parent::__construct();
     90
     91        add_filter('plugin_action_links_acf-galerie-4/acf-galerie-4.php', array($this, 'add_custom_action_link'));
     92    }
     93
     94    /**
     95     * Add a custom action link next to "Deactivate" on the plugins page.
     96     *
     97     * @param array $links An array of existing action links.
     98     * @return array Modified array of action links.
     99     */
     100    function add_custom_action_link($links) {
     101        // Add your custom link
     102        $custom_link = '<a href="#" id="acfg4-migrate">Migrate</a>';
     103       
     104        // Append the custom link to the end of the array
     105        $links[] = $custom_link;
     106   
     107        return $links;
    90108    }
    91109
     
    220238     */
    221239    function format_value( $value, $post_id, $field ) {
     240        if( empty( $value ) ) return array();
     241
    222242        $attachment_ids = array_map( 'intval', $value );
    223243
    224         if( empty( $attachment_ids ) ){
    225             return array();
    226         }
     244        if( empty( $attachment_ids ) ) return array();
    227245
    228246        return $this->transform( $attachment_ids );
  • acf-galerie-4/trunk/readme.txt

    r3197857 r3218193  
    55Requires at least: 5.8
    66Tested up to: 6.7
    7 Stable tag: 1.3.1
     7Stable tag: 1.3.2
    88Requires PHP: 7.0
    99License: GPLv2 or later
     
    3939
    4040== Changelog ==
     41= 1.3.2 =
     42* [Added] Migrate from ACF Gallery Field or ACF Gallery Pro to ACF Galerie 4.
     43
    4144= 1.3.1 =
    4245* [Fixed] WordPress media library not opening in user edit screen
Note: See TracChangeset for help on using the changeset viewer.