|
139 | 139 | continue;
|
140 | 140 | }
|
141 | 141 |
|
| 142 | + $is_multilingual = function_exists('pll_get_post_translations'); |
| 143 | + $languages = array(); |
| 144 | + if ($is_multilingual) { |
| 145 | + $languages_raw = get_terms('term_language', ['hide_empty' => false]); |
| 146 | + foreach ($languages_raw as $lang) { |
| 147 | + $languages[str_replace('pll_', '', $lang->slug)] = $lang->name; |
| 148 | + } |
| 149 | + } |
| 150 | + |
142 | 151 | /* Create post */
|
143 | 152 | $random_images = get_posts(array(
|
144 | 153 | 'post_type' => 'attachment',
|
|
156 | 165 | /* Add images */
|
157 | 166 | $post_title = $raw_titles[mt_rand(0, $nb_raw_titles - 1)] . ' ' . $label . ' #' . $i;
|
158 | 167 |
|
159 |
| - $post_id = wp_insert_post(array( |
| 168 | + $post_infos = array( |
160 | 169 | 'post_title' => $post_title,
|
161 | 170 | 'post_content' => $raw_contents[mt_rand(0, $nb_raw_contents - 1)],
|
162 | 171 | 'post_type' => $pt,
|
163 | 172 | 'post_status' => 'publish',
|
164 | 173 | 'post_author' => 1
|
165 |
| - )); |
| 174 | + ); |
| 175 | + |
| 176 | + $post_id = wp_insert_post($post_infos); |
166 | 177 | $_hasImport = true;
|
167 | 178 |
|
168 | 179 | /* Taxonomies */
|
| 180 | + $thumbnail_id = false; |
169 | 181 | if ($post_id && !empty($random_images)) {
|
170 |
| - set_post_thumbnail($post_id, $random_images[array_rand($random_images)]); |
| 182 | + $thumbnail_id = $random_images[array_rand($random_images)]; |
| 183 | + set_post_thumbnail($post_id, $thumbnail_id); |
| 184 | + } |
| 185 | + |
| 186 | + if ($is_multilingual && $languages) { |
| 187 | + $first_code = array_keys($languages)[0]; |
| 188 | + $translations = array($post_id); |
| 189 | + foreach ($languages as $lang_code => $lang_name) { |
| 190 | + if ($lang_code === $first_code) { |
| 191 | + continue; |
| 192 | + } |
| 193 | + $post_infos['post_title'] = $post_title . ' (' . $lang_name . ')'; |
| 194 | + $translated_post_id = wp_insert_post($post_infos); |
| 195 | + if ($translated_post_id) { |
| 196 | + pll_set_post_language($translated_post_id, $lang_code); |
| 197 | + pll_save_post_translations(array_merge(pll_get_post_translations($post_id), [$lang_code => $translated_post_id])); |
| 198 | + } |
| 199 | + if($thumbnail_id){ |
| 200 | + set_post_thumbnail($translated_post_id, $thumbnail_id); |
| 201 | + } |
| 202 | + } |
171 | 203 | }
|
172 | 204 |
|
173 | 205 | foreach ($taxonomies as $tax_name) {
|
|
0 commit comments