Plugin Directory

Changeset 2849254


Ignore:
Timestamp:
01/16/2023 03:34:43 PM (3 years ago)
Author:
slickplan
Message:

Add language selector

Location:
slickplan-importer/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • slickplan-importer/trunk/readme.txt

    r2790191 r2849254  
    33Donate link: https://slickplan.com/
    44Tags: slickplan, import, xml
    5 Requires at least: 3.0
    6 Tested up to: 6.0.2
     5Requires at least: 4.0
     6Tested up to: 6.1.1
    77Stable tag: trunk
    88License: GPL-3.0
  • slickplan-importer/trunk/slickplan.php

    r2790191 r2849254  
    66Author: Slickplan.com <[email protected]>
    77Author URI: https://slickplan.com/
    8 Version: 2.2.2
     8Version: 2.2.3
    99License: GPL-3.0 - https://www.gnu.org/licenses/gpl-3.0.html
    1010*/
     
    296296                    'titles' => $form['titles_change'] ?? '',
    297297                    'content' => $form['content'] ?? '',
     298                    'content_lang' => $form['content_lang'] ?? '',
    298299                    'content_files' => (
    299300                        isset($form['content'], $form['content_files'])
     
    318319                    $this->_options['create_menu'] = false;
    319320                }
     321                if (isset($form['content_lang']) && $form['content_lang']) {
     322                    foreach ($xml['pages'] as $pageId => $pageData) {
     323                        if (
     324                            isset($xml['pages'][$pageId]['contents'])
     325                            && is_array($xml['pages'][$pageId]['contents'])
     326                            && array_keys($xml['pages'][$pageId]['contents']) === range(0, count($xml['pages'][$pageId]['contents']) - 1)
     327                        ) {
     328                            $xml['pages'][$pageId]['contents'] = array_reduce($xml['pages'][$pageId]['contents'], function ($result, $item) use ($form) {
     329                                if ($item['@attributes']['lang'] === $form['content_lang']) {
     330                                    return $item;
     331                                }
     332                                return $result;
     333                            }, []);
     334                        }
     335                    }
     336                }
    320337                if ($this->_options['content_files']) {
    321338                    // Redirect to AJAX importer
     
    324341                    wp_redirect($this->_getAdminUrl('import'));
    325342                } else {
     343                    if (isset($form['content_lang']) && $form['content_lang']) {
     344                        update_option(SLICKPLAN_PLUGIN_OPTION, $xml);
     345                    }
     346
    326347                    // There is no files, so we can import pages faster than using AJAX importer
    327348                    foreach (['home', '1', 'util', 'foot'] as $type) {
     
    342363            }
    343364
     365            $allLanguages = $this->_getLanguages();
     366            $content_languages = [];
    344367            $no_of_files = 0;
    345368            $filesize_total = [];
    346369            if (isset($xml['pages']) and is_array($xml['pages'])) {
    347370                foreach ($xml['pages'] as $page) {
    348                     if (isset($page['contents']['body']) and is_array($page['contents']['body'])) {
    349                         foreach ($page['contents']['body'] as $body) {
    350                             if (isset($body['type']) and ($body['type'] === 'image' or $body['type'] === 'video' or $body['type'] === 'file')) {
    351                                 foreach ($this->_getMediaElementArray($body) as $item) {
    352                                     if (isset($item['type']) and $item['type'] === 'library') {
    353                                         ++$no_of_files;
    354                                     }
    355                                     if (isset($item['file_size'], $item['file_id']) and $item['file_size']) {
    356                                         $filesize_total[$item['file_id']] = (int)$item['file_size'];
    357                                     }
    358                                 }
     371                    if (isset($page['contents'][0]['@attributes']['lang'])) {
     372                        foreach ($page['contents'] as $row) {
     373                            if (isset($row['@attributes']['lang'], $allLanguages[$row['@attributes']['lang']]['name']) and $row['@attributes']['lang']) {
     374                                $content_languages[$row['@attributes']['lang']] = $allLanguages[$row['@attributes']['lang']]['name'];
    359375                            }
    360                         }
     376                            if (isset($row['body']) && is_array($row['body'])) {
     377                                $filesData = $this->_sumFiles($row['body']);
     378                                $no_of_files += $filesData[0];
     379                                $filesize_total = array_merge($filesize_total, $filesData[1]);
     380                            }
     381                        }
     382                    } elseif (isset($page['contents']['body']) and is_array($page['contents']['body'])) {
     383                        $filesData = $this->_sumFiles($page['contents']['body']);
     384                        $no_of_files += $filesData[0];
     385                        $filesize_total = array_merge($filesize_total, $filesData[1]);
    361386                    }
    362387                }
     
    10781103
    10791104        /**
     1105         * @param $name
     1106         * @param string $label
     1107         * @param $options
     1108         * @return string
     1109         */
     1110        public function displayDropdown($name, string $label, $options): string {
     1111            $id = sanitize_title('slickplan-importer-form-' . $name);
     1112            $attrs = [
     1113                'name' => 'slickplan_importer[' . $name . ']',
     1114                'id' => $id,
     1115            ];
     1116
     1117            $html = '<label for="' . $id . '">' . $label . ':</label> <select';
     1118            foreach ($attrs as $key => $value) {
     1119                $html .= ' ' . $key . '="' . esc_attr($value) . '"';
     1120            }
     1121            $html .= '>';
     1122            foreach ($options as $value => $name) {
     1123                $html .= '<option value="' . esc_attr($value) . '">' . esc_attr($name) . '</option>';
     1124            }
     1125            $html .= '</select>';
     1126            return $html;
     1127        }
     1128
     1129        /**
    10801130         * Display checkbox.
    10811131         *
     
    15101560
    15111561        /**
     1562         * @param $bodyItems
     1563         * @return array
     1564         */
     1565        private function _sumFiles($bodyItems)
     1566        {
     1567            $no_of_files = 0;
     1568            $filesize_total = [];
     1569            foreach ($bodyItems as $body) {
     1570                if (isset($body['type']) and ($body['type'] === 'image' or $body['type'] === 'video' or $body['type'] === 'file')) {
     1571                    foreach ($this->_getMediaElementArray($body) as $item) {
     1572                        if (isset($item['type']) and $item['type'] === 'library') {
     1573                            ++$no_of_files;
     1574                        }
     1575                        if (isset($item['file_size'], $item['file_id']) and $item['file_size']) {
     1576                            $filesize_total[$item['file_id']] = (int)$item['file_size'];
     1577                        }
     1578                    }
     1579                }
     1580            }
     1581            return [$no_of_files, $filesize_total];
     1582        }
     1583
     1584        private function _getLanguages()
     1585        {
     1586            return json_decode(file_get_contents(SLICKPLAN_PLUGIN_PATH . 'assets/languages.json'), true);
     1587        }
     1588
     1589        /**
    15121590         * @return bool
    15131591         */
  • slickplan-importer/trunk/views/options.php

    r2553277 r2849254  
    100100                        true
    101101                    );
     102                    if (isset($content_languages) and count($content_languages) > 1) {
     103                        $radio .= '<br class="content-suboption-br">'
     104                            . '<span class="content-suboption" style="display: inline-block; padding: 3px 0 0 20px;">'
     105                            . $this->displayDropdown(
     106                                'content_lang',
     107                                'Language',
     108                                $content_languages
     109                            )
     110                            . '</span>';
     111                    }
    102112                    if (isset($no_of_files) and $no_of_files) {
    103113                        $radio .= '<br class="content-suboption-br">'
Note: See TracChangeset for help on using the changeset viewer.