Plugin Directory

Changeset 3084521


Ignore:
Timestamp:
05/10/2024 12:10:43 PM (23 months ago)
Author:
opcodespace
Message:

background process

Location:
simple-export-import-for-acf-data/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • simple-export-import-for-acf-data/trunk/app.php

    r3078598 r3084521  
    55Plugin URI: https://www.opcodespace.com
    66Author: Opcodespace <mehedee@opcodespace.com>
    7 Version: 1.3.17
     7Version: 1.3.18
    88Text Domain: simple-export-import-for-acf-data
    99*/
     
    1313define("SEIP_VIEW_PATH", wp_normalize_path(plugin_dir_path(__FILE__) . "view/"));
    1414define("SEIP_ASSETSURL", plugins_url("assets/", __FILE__));
    15 define('SEIP_PLUGIN_VERSION', '1.3.17');
     15define('SEIP_PLUGIN_VERSION', '1.3.18');
    1616define('PAID_TEXT', '<small class="paid_text">(This is for paid user)</small>');
    1717
  • simple-export-import-for-acf-data/trunk/readme.txt

    r3078598 r3084521  
    55Tested up to: 6.5.2
    66Requires PHP: 7.0
    7 Stable tag: 1.3.17
     7Stable tag: 1.3.18
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    9696
    9797== Changelog ==
     98= 1.3.18 (May 10, 2024) =
     99* Enhancement: Bulk import in background for large data
     100
    98101= 1.3.17 (April 23, 2024) =
    99102* Enhancement: Splitting Bulk Export
  • simple-export-import-for-acf-data/trunk/src/SeipEnqueue.php

    r2997056 r3084521  
    3131            wp_localize_script(
    3232                'admin-script',
    33                 'frontend_form_object',
     33                'seip_frontend_form_object',
    3434                array(
    35                     'ajaxurl' => admin_url('admin-ajax.php')
     35                    'ajaxurl' => admin_url('admin-ajax.php'),
     36                    'seip_background_import_status' => get_option('seip_background_import_status'),
    3637                )
    3738            );
  • simple-export-import-for-acf-data/trunk/src/SeipExport.php

    r3078598 r3084521  
    8585
    8686            return [
     87                'ID'             => $post->ID,
    8788                'post_date'      => $post->post_date,
    8889                'post_title'     => $post->post_title,
  • simple-export-import-for-acf-data/trunk/src/SeipFront.php

    r3078598 r3084521  
    1414            add_action('wp_ajax_seip_get_all_terms', [$self, 'seip_get_all_terms']);
    1515            add_action('wp_ajax_seip_save_license_key', [$self, 'seip_save_license_key']);
     16            add_action('wp_ajax_seip_banckground_import', [$self, 'seip_banckground_import']);
    1617
    1718        }
     
    140141
    141142        }
     143
     144        public function seip_banckground_import()
     145        {
     146            if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'seip_export_import' ) ) {
     147                wp_send_json_error(['message' => 'You are not allowed to submit data.']);
     148            }
     149
     150            (new SeipImport())->seip_import_background();
     151        }
    142152    }
    143153}
  • simple-export-import-for-acf-data/trunk/src/SeipImport.php

    r3049001 r3084521  
    6666            }
    6767
    68             $post_id  = (int) $_POST['post_id'];
     68            $post_id = (int)$_POST['post_id'];
    6969            $settings = [
    70                 'bulk_import'           => isset($_POST['bulk_import']),
    71                 'update_post_page_ttl'  => isset($_POST['update_post_page_ttl']),
     70                'bulk_import' => isset($_POST['bulk_import']),
     71                'update_post_page_ttl' => isset($_POST['update_post_page_ttl']),
    7272                'update_post_page_slug' => isset($_POST['update_post_page_slug']),
    73                 'single_post_id'        => $post_id,
    74                 'post_type'             => sanitize_text_field($_POST['post_type'])
     73                'single_post_id' => $post_id,
     74                'post_type' => sanitize_text_field($_POST['post_type'])
    7575            ];
    7676
     
    7979            if (isset($_POST['bulk_import'])) {
    8080                $this->execution_time = time();
     81
     82                if (isset($_POST['background_import'])) {
     83                    $result = $this->saveOnDisk($posts);
     84                    if ($result) {
     85                        update_option('seip_background_import_status', 'processing');
     86                        update_option('seip_bulk_settings', $settings);
     87                        seip_notices_with_redirect('msg1', __('Importing data in background', 'simple-export-import-for-acf-data'),
     88                            'success');
     89                    }
     90                    else {
     91                        seip_notices_with_redirect('msg1', __('Error while saving file', 'simple-export-import-for-acf-data'),
     92                            'error');
     93                    }
     94                }
    8195            }
    8296
    8397            foreach ($posts as $post) {
    84                 if(empty($post)){
     98                if (empty($post)) {
    8599                    continue;
    86100                }
     
    92106            seip_notices_with_redirect('msg1', __('Successfully imported', 'simple-export-import-for-acf-data'),
    93107                'success');
     108        }
     109
     110        public function seip_import_background()
     111        {
     112            $posts = $this->readFromDisk();
     113
     114            if (empty($posts)) {
     115                delete_option('seip_background_import_status');
     116                delete_option('seip_bulk_settings');
     117                wp_send_json_success(['imported_posts' => [], 'message' => 'No data found']);
     118            }
     119
     120            $settings = get_option('seip_bulk_settings');
     121
     122            $cnt = 0;
     123            $imported_posts = [];
     124            foreach ($posts as $key => $post) {
     125                if (empty($post)) {
     126                    continue;
     127                }
     128
     129                if($cnt > 5){
     130                    break;
     131                }
     132
     133                $this->post_data($post, $settings);
     134
     135                $imported_posts[] = $post['ID'];
     136
     137                unset($posts[$key]);
     138
     139                $cnt++;
     140            }
     141
     142            $this->saveOnDisk($posts);
     143
     144            wp_send_json_success(['imported_posts' => $imported_posts]);
    94145        }
    95146
     
    121172                $SeipImportEditorMediaFile = new SeipImportEditorMediaFile($data['source_domain']);
    122173                $content = wp_kses_post($SeipImportEditorMediaFile->download_editor_media_files($data['post_content']));
    123             }
    124             else{
     174            } else {
    125175                $content = wp_kses_post($data['post_content']);
    126176            }
    127177
    128178            $post_data = [
    129                 'post_content'  => $content,
    130                 'post_status'   => sanitize_text_field($data['post_status']),
    131                 'post_excerpt'  => sanitize_textarea_field($data['post_excerpt']),
     179                'post_content' => $content,
     180                'post_status' => sanitize_text_field($data['post_status']),
     181                'post_excerpt' => sanitize_textarea_field($data['post_excerpt']),
    132182                'post_password' => sanitize_text_field($data['post_password']),
    133183            ];
     
    150200
    151201                $post = get_posts([
    152                     'name'      => sanitize_title($data['post_name']),
     202                    'name' => sanitize_title($data['post_name']),
    153203                    'post_type' => sanitize_text_field($settings['post_type'])
    154204                ]);
     
    156206                if (empty($post)) {
    157207                    $primary_data = [
    158                         'post_date'     => sanitize_text_field($data['post_date']),
    159                         'post_content'  => $content,
    160                         'post_title'    => sanitize_text_field($data['post_title']),
    161                         'post_status'   => sanitize_text_field($data['post_status']),
    162                         'post_excerpt'  => sanitize_textarea_field($data['post_excerpt']),
     208                        'post_date' => sanitize_text_field($data['post_date']),
     209                        'post_content' => $content,
     210                        'post_title' => sanitize_text_field($data['post_title']),
     211                        'post_status' => sanitize_text_field($data['post_status']),
     212                        'post_excerpt' => sanitize_textarea_field($data['post_excerpt']),
    163213                        'post_password' => sanitize_text_field($data['post_password']),
    164                         'post_type'     => sanitize_text_field($settings['post_type'])
     214                        'post_type' => sanitize_text_field($settings['post_type'])
    165215                    ];
    166216
    167217                    $post_id = wp_insert_post($primary_data);
    168218                } else {
    169                     $post_id         = $post[0]->ID;
     219                    $post_id = $post[0]->ID;
    170220                    $post_data['ID'] = $post_id;
    171221                    wp_update_post($post_data);
    172222                }
    173223            } else {
    174                 $post_id         = (int) $settings['single_post_id'];
     224                $post_id = (int)$settings['single_post_id'];
    175225                $post_data['ID'] = $post_id;
    176226
     
    181231
    182232            $this->current_post_id = $post_id;
    183             $this->post_metas      = $data['metas'];
    184 
    185             if(isset($data['metas']) && !empty($data['metas'])){
     233            $this->post_metas = $data['metas'];
     234
     235            if (isset($data['metas']) && !empty($data['metas'])) {
    186236                foreach ($data['metas'] as $key => $value) {
    187237                    update_post_meta($post_id, $key, $this->get_field_value($key, $value));
     
    190240
    191241            # Adding Featured image
    192             $featured_image = (array) $data['featured_image'];
     242            $featured_image = (array)$data['featured_image'];
    193243
    194244            if (!empty($featured_image) && isset($featured_image['url'])) {
     
    198248
    199249            # Setting Terms
    200             if(!empty($data['terms'])){
     250            if (!empty($data['terms'])) {
    201251                $this->set_terms($post_id, $data['terms']);
    202252            }
     
    214264            }
    215265
    216             if(empty($value)){
     266            if (empty($value)) {
    217267                return $value;
    218268            }
    219269
    220             if (!isset($this->post_metas['_'.$key]) || empty($this->post_metas['_'.$key])) {
     270            if (!isset($this->post_metas['_' . $key]) || empty($this->post_metas['_' . $key])) {
    221271                return $value;
    222272            }
    223273
    224             $keys = explode('_field_', $this->post_metas['_'.$key]);
     274            $keys = explode('_field_', $this->post_metas['_' . $key]);
    225275            $no_of_keys = count($keys);
    226             if($no_of_keys > 1){
    227                 $related_field = get_field_object('field_'.$keys[$no_of_keys - 1]);
    228             }
    229             else{
    230                 $related_field = get_field_object($this->post_metas['_'.$key]);
     276            if ($no_of_keys > 1) {
     277                $related_field = get_field_object('field_' . $keys[$no_of_keys - 1]);
     278            } else {
     279                $related_field = get_field_object($this->post_metas['_' . $key]);
    231280            }
    232281
     
    249298                $total_uploaded_images = $seip_settings['import_images'];
    250299
    251                 if($total_uploaded_images >= 10){
     300                if ($total_uploaded_images >= 10) {
    252301                    return $value;
    253302                }
     
    255304                $total_uploaded_images++;
    256305                $seip_settings['import_images'] = $total_uploaded_images;
    257                 update_option( 'seip_settings', $seip_settings);
     306                update_option('seip_settings', $seip_settings);
    258307
    259308                $upload = $this->download($value['url']);
     
    289338                $new_images = [];
    290339                foreach ($images as $image) {
    291                     if(empty($image['url'])){
     340                    if (empty($image['url'])) {
    292341                        continue;
    293342                    }
    294                     $upload       = $this->download($image['url']);
     343                    $upload = $this->download($image['url']);
    295344                    $new_images[] = $this->attach($upload, $image);
    296345                }
     
    316365                $value,
    317366                array(
    318                     'timeout'  => 300,
     367                    'timeout' => 300,
    319368                    'filename' => basename($value)
    320369                )
     
    322371
    323372            $response_code = wp_remote_retrieve_response_code($response);
    324             $content       = wp_remote_retrieve_body($response);
     373            $content = wp_remote_retrieve_body($response);
    325374
    326375            if ($response_code != 200) {
     
    352401            $attachment = array(
    353402                'post_mime_type' => $upload['type'],
    354                 'guid'           => $upload['url'],
    355                 'post_title'     => empty($media_data['post_title']) ? sanitize_title(basename($upload['file'])) : sanitize_text_field($media_data['post_title']),
    356                 'post_content'   => isset($media_data['post_content']) ? sanitize_text_field($media_data['post_content']) : '',
    357                 'post_excerpt'   => isset($media_data['post_excerpt']) ? sanitize_text_field($media_data['post_excerpt']) : '',
    358                 'post_parent'    => $this->current_post_id > 0 ? $this->current_post_id : 0,
     403                'guid' => $upload['url'],
     404                'post_title' => empty($media_data['post_title']) ? sanitize_title(basename($upload['file'])) : sanitize_text_field($media_data['post_title']),
     405                'post_content' => isset($media_data['post_content']) ? sanitize_text_field($media_data['post_content']) : '',
     406                'post_excerpt' => isset($media_data['post_excerpt']) ? sanitize_text_field($media_data['post_excerpt']) : '',
     407                'post_parent' => $this->current_post_id > 0 ? $this->current_post_id : 0,
    359408            );
    360             $attach_id  = wp_insert_attachment($attachment, $upload['file']);
     409            $attach_id = wp_insert_attachment($attachment, $upload['file']);
    361410
    362411            if (is_wp_error($attach_id)) {
     
    433482        protected function link_field($value)
    434483        {
    435             if(empty($value['link']['url'])){
     484            if (empty($value['link']['url'])) {
    436485                return $value;
    437486            }
    438             $url         = str_replace($value['source_domain'], home_url(), $value['link']['url']);
    439             $link        = $value['link'];
     487            $url = str_replace($value['source_domain'], home_url(), $value['link']['url']);
     488            $link = $value['link'];
    440489            $link['url'] = $url;
    441490            return $link;
    442491        }
     492
     493        protected function saveOnDisk($posts)
     494        {
     495            $upload_dir = wp_upload_dir();
     496            $file_name = 'seip_export_' . time() . '.json';
     497            $file_path = $upload_dir['basedir'] . '/seip/' . $file_name;
     498            $file_url = $upload_dir['baseurl'] . '/seip/' . $file_name;
     499
     500            if (!file_exists($upload_dir['basedir'] . '/seip')) {
     501                wp_mkdir_p($upload_dir['basedir'] . '/seip');
     502            }
     503
     504            update_option('seip_export_file', $file_path);
     505            $result = file_put_contents($file_path, json_encode($posts));
     506
     507            return $result ? $file_url : $result;
     508        }
     509
     510        protected function readFromDisk()
     511        {
     512            $file_path = get_option('seip_export_file');
     513            return wp_json_file_decode($file_path, ['associative' => true]);
     514        }
    443515    }
    444 
    445516}
  • simple-export-import-for-acf-data/trunk/view/_import.php

    r2989857 r3084521  
    1717                                   name="bulk_import" <?php echo !SeipOpcodespace::isPaid() ? 'disabled' : '' ?>><label
    1818                                    for="bulk_import" class="checkbox_label">Bulk Import</label> <br>
    19                             <?php echo !SeipOpcodespace::isPaid() ? PAID_TEXT : '(If slug is matched, it will update that post/page. Otherwise, it creates a new post.)' ?>
     19                            <?php echo !SeipOpcodespace::isPaid() ? PAID_TEXT : 'If slug is matched, it will update that post/page. Otherwise, it creates a new post.' ?>
    2020                        </div>
    2121                        <div class="block_imports">
     
    8989                                </tr>
    9090                                <tr class="">
    91                                     <td><label for="file">Upload File</label></td>
     91                                    <td><label for="file" class="label_block">Upload File</label></td>
    9292                                    <td>
    9393                                        <input type="file" name="file" id="file" accept="application/json">
    9494                                        <br><span class="description">Only JSON format is supported.</span>
     95                                    </td>
     96                                </tr>
     97                                <tr class="">
     98                                    <td colspan="2">
     99                                        <div>
     100                                            <input type="checkbox" id="background_import" name="background_import">
     101                                            <label for="background_import"><strong>Import in Background</strong></label>
     102                                        </div>
     103                                        <p>If you have a large number of posts and images, and you have a timeout issue, you can select background upload. You should open this browser window until complete the process. Otherwise, importing will not work.</p></td>
     104                                </tr>
     105                                <tr>
     106                                    <td></td>
     107                                    <td>
     108
    95109                                    </td>
    96110                                </tr>
  • simple-export-import-for-acf-data/trunk/view/_license.php

    r2758968 r3084521  
    2929            $.ajax({
    3030                method: 'POST',
    31                 url: frontend_form_object.ajaxurl,
     31                url: seip_frontend_form_object.ajaxurl,
    3232                data: { action: 'seip_save_license_key', _wpnonce: $('#_wpnonce').val(), seip_license_key: $('[name="seip_license_key"]').val()}
    3333            })
  • simple-export-import-for-acf-data/trunk/view/export_import.php

    r3078598 r3084521  
    5252    }
    5353
     54    .simple_imported_items {
     55        background: #000;
     56        padding: 15px;
     57        color: green;
     58        height: 150px;
     59        display: block;
     60        overflow-y: scroll;
     61    }
     62
     63    .loading_animation {
     64        display: flex;
     65    }
     66
     67    .loading_animation svg path,
     68    .loading_animation svg rect {
     69        fill: #FF6700;
     70    }
     71
    5472    /* Tablet Layout: 768px. */
    5573    @media only screen and (min-width: 768px) and (max-width: 991px) {
     
    7694<div class="export_import_wrapper">
    7795    <h5 class="main_title">Simple Export Import for ACF Data</h5>
     96
     97    <!--    // Background Process-->
     98    <?php if (get_option('seip_background_import_status') === 'processing'): ?>
     99        <div class="notice notice-warning">
     100            <div class="loading_animation">
     101                <div class="loader" title="0">
     102                    <svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg"
     103                         xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     104                         width="40px" height="40px" viewBox="0 0 40 40" enable-background="new 0 0 40 40"
     105                         xml:space="preserve">
     106                  <path opacity="0.2" fill="#000" d="M20.201,5.169c-8.254,0-14.946,6.692-14.946,14.946c0,8.255,6.692,14.946,14.946,14.946
     107                  s14.946-6.691,14.946-14.946C35.146,11.861,28.455,5.169,20.201,5.169z M20.201,31.749c-6.425,0-11.634-5.208-11.634-11.634
     108                  c0-6.425,5.209-11.634,11.634-11.634c6.425,0,11.633,5.209,11.633,11.634C31.834,26.541,26.626,31.749,20.201,31.749z"/>
     109                        <path fill="#000" d="M26.013,10.047l1.654-2.866c-2.198-1.272-4.743-2.012-7.466-2.012h0v3.312h0
     110                  C22.32,8.481,24.301,9.057,26.013,10.047z">
     111                            <animateTransform attributeType="xml"
     112                                              attributeName="transform"
     113                                              type="rotate"
     114                                              from="0 20 20"
     115                                              to="360 20 20"
     116                                              dur="0.5s"
     117                                              repeatCount="indefinite"/>
     118                        </path>
     119          </svg>
     120                </div>
     121                <p style="color: tomato; font-weight: bold">Importing process is running. Please don't close
     122                    this window.</p>
     123            </div>
     124            <div class="seip_background_import_status">
     125                <ul class="simple_imported_items">
     126
     127                </ul>
     128            </div>
     129        </div>
     130    <?php endif; ?>
     131    <!--  // End Background Process  -->
     132
    78133    <?php $tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'export' ?>
    79134    <nav class="nav-tab-wrapper">
     
    164219
    165220
     221        if (seip_frontend_form_object.seip_background_import_status === 'processing') {
     222            seip_banckground_import();
     223        }
     224
     225        function seip_banckground_import() {
     226            $.ajax({
     227                method: "POST",
     228                url: "<?php echo esc_url(admin_url('/admin-ajax.php')); ?>",
     229                data: {
     230                    action: "seip_banckground_import",
     231                    _wpnonce: $('#seip_export_import_nonce').val()
     232                }
     233            })
     234                .done(function (response) {
     235                    if (response.success) {
     236                        if (response.data.imported_posts.length > 0) {
     237                            response.data.imported_posts.map(post => {
     238                                $('.seip_background_import_status ul').append(`<li>Imported: #${post}</li>`);
     239                            })
     240                            $(".seip_background_import_status ul").scrollTop($(".seip_background_import_status ul")[0].scrollHeight);
     241                            seip_banckground_import();
     242                        } else {
     243                            $('.seip_background_import_status ul').append(`<li>Completed</li>`);
     244                            $('.export_import_wrapper .loading_animation').html('<p style="color: green; font-weight: bold">Importing process is completed.</p>');
     245                        }
     246                    }
     247                });
     248        }
     249
     250
    166251        function seip_get_all_posts(_this) {
    167252            let _this_parent = _this.parents('form');
     
    194279                        _this_parent.find('[name="post_id"]').html(options).trigger("chosen:updated");
    195280
    196                         if($('#export_mulit_pages').length > 0){
     281                        if ($('#export_mulit_pages').length > 0) {
    197282                            $('#export_mulit_pages').html(options);
    198283
     
    259344        }
    260345
    261         $('[name="terms[]"]').on('change',function () {
     346        $('[name="terms[]"]').on('change', function () {
    262347            seip_get_all_posts($(this));
    263348        })
Note: See TracChangeset for help on using the changeset viewer.