PluginProbe
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export / trunk
Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export vtrunk
3.0 2.24.2 2.24.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 2.0 2.0.1 2.1 2.1.1 2.1.2 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.16.1 2.16.2 All 96 releases
wp-ultimate-exporter / exportExtensions / JetCustomTableExport.php

JetCustomTableExport.php in Export All Posts, Products, Orders & Users | WP Ultimate Exporter | WordPress CSV Export trunk, at exportExtensions/JetCustomTableExport.php

380 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Ultimate Exporter plugin file.
4 *
5 * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com
6 */
7 namespace Smackcoders\SMEXP;
8
9 use Smackcoders\WCSV\WC_Coupon;
10
11 if (! defined('ABSPATH'))
12 exit; // Exit if accessed directly
13
14 /**
15 * Class JetCustomTableExport
16 * @package Smackcoders\WCSV
17 */
18 class JetCustomTableExport
19 {
20 protected static $instance = null, $mapping_instance, $export_handler, $export_instance, $post_export;
21 public $totalRowCount;
22 public $plugin;
23
24 public static function getInstance()
25 {
26 if (null == self::$instance) {
27 self::$instance = new self;
28 JetCustomTableExport::$export_instance = ExportExtension::getInstance();
29 JetCustomTableExport::$post_export = PostExport::getInstance();
30 }
31 return self::$instance;
32 }
33
34 /**
35 * JetBookingExport constructor.
36 */
37 public function __construct()
38 {
39 $this->plugin = Plugin::getInstance();
40 }
41
42 public function get_custom_table_meta_fields($module,$id, $table_name, $optionalType)
43 {
44 global $wpdb;
45 $meta_fields = jet_engine()->meta_boxes->get_meta_fields_for_object($optionalType);
46 $meta_key = $meta_value = [];
47 $field_types = [];
48 $query = "SELECT * FROM $table_name WHERE object_ID = %d";
49 $get_cpt_fields = $wpdb->get_results($wpdb->prepare($query, $id), ARRAY_A);
50
51 $jet_rep_cptfields = [];
52 $jet_rep_cpttypes = [];
53
54 // Convert meta fields into an associative array [field_name => type]
55 foreach ($meta_fields as $meta) {
56 if (isset($meta['name'], $meta['type'])) {
57 $field_types[$meta['name']] = $meta['type'];
58 }
59 }
60
61 foreach ($meta_fields as $meta) {
62 $jet_cptfield_names = $meta['name'];
63 $jet_field_type = $meta['type'];
64
65 if ($jet_field_type === 'repeater' && isset($meta['repeater-fields'])) {
66 foreach ($meta['repeater-fields'] as $rep_field) {
67 if (isset($rep_field['name'], $rep_field['type'])) {
68 $jet_rep_cptfields[$rep_field['name']] = $rep_field['name'];
69 $jet_rep_cpttypes[$rep_field['name']] = $rep_field['type'];
70 }
71 }
72 }
73 }
74
75 // Store repeater fields in the export instance
76 $jet_rep_cptfields = !empty($jet_rep_cptfields) ? $jet_rep_cptfields : [];
77 $jet_rep_cpttypes = !empty($jet_rep_cpttypes) ? $jet_rep_cpttypes : [];
78 foreach ($meta_fields as $meta) {
79 if (isset($meta['name'], $meta['type'])) {
80 $field_types[$meta['name']] = $meta['type'];
81 }
82 }
83 // Process and export data
84 foreach ($get_cpt_fields as $row) {
85 foreach ($row as $meta_key => $meta_value) {
86 if (isset($field_types[$meta_key])) {
87 $field_type = $field_types[$meta_key];
88 if ($field_type === 'checkbox') {
89 $checkValue = unserialize($meta_value);
90 $check = '';
91 foreach($checkValue as $checkkey => $metvalue){
92 if(is_numeric($checkkey)){
93 $check .= $metvalue.',';
94 $rcheck = substr($check,0,-1);
95 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $rcheck;
96 }
97 else{
98 if($metvalue == 'true'){
99
100 $exp_value[] = $checkkey;
101 }
102 if(isset($exp_value) && is_array($exp_value)){
103 $meta_value = implode(',',$exp_value );
104 }
105 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
106 }
107 }
108 }
109 elseif ($field_type === 'gallery') {
110 $gallery= explode(',',$meta_value);
111 foreach($gallery as $gallerykey => $galleryval){
112 if(is_numeric($galleryval)){
113 $galleries[] = wp_get_attachment_url( $galleryval );
114 }
115 elseif(is_serialized($galleryval)){
116 $gal_value=unserialize($galleryval);
117 foreach($gal_value as $key=>$gal_val){
118 if(is_array($gal_val)){
119 $galleries[] = $gal_val['url'];
120 }
121 else{
122 $galleries[] = $gal_val;
123 }
124
125 }
126 }
127 else{
128 $galleries[] = $galleryval;
129 }
130 if(is_array($galleries)){
131 $meta_value =!empty($galleries) ? implode(',',$galleries ) : '';
132 }
133 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
134 }
135 }
136 elseif ($field_type === 'media') {
137 $array_val= $meta_value;
138 if(is_numeric($array_val)){
139 $meta_value = wp_get_attachment_url( $array_val );
140 }
141 elseif(is_serialized($array_val)){
142 $media_value=unserialize($array_val);
143 $meta_value = array_key_exists('url',$media_value) ? $media_value['url'] : "";
144
145 }
146 else{
147 $meta_value=$array_val;
148 }
149 JetCustomTableExport::$export_instance->data[$id][$meta_key] = $meta_value;
150 }
151 elseif ($field_type === 'select') {
152 if(is_serialized($meta_value)){
153 $meta_value = unserialize($meta_value);
154 foreach($meta_value as $metkey => $metselectvalue){
155 $select[] = $metselectvalue;
156 $meta_value = !empty($select) ? implode(',',$select ) : '';
157 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
158 }
159 }
160 else{
161 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
162 }
163 }
164 elseif($field_type === 'posts') {
165 if(is_serialized($meta_value)){
166 $meta_value = unserialize($meta_value);
167 foreach($meta_value as $postkey => $metpostvalue){
168 if(is_numeric($metpostvalue)){
169 $title = $wpdb->get_row($wpdb->prepare("select post_title from {$wpdb->prefix}posts where ID = %d",$metpostvalue));
170 $test[] = $title->post_title;
171 }
172 }
173 $meta_value = !empty($test) ? implode(',',$test ) : '';
174 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
175 }
176 else{
177 $post_value = $meta_value;
178 $post_title = $wpdb->get_var("SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = $post_value");
179 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $post_title;
180 }
181 }
182 elseif($field_type == 'date'){
183 if(!empty($meta_value)){
184 if(strpos($meta_value, '-') !== FALSE){
185 }else{
186 if(is_numeric($meta_value)){
187 $meta_value = date('Y-m-d', $meta_value);
188 }
189 }
190 }
191 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
192 }
193 elseif($field_type == 'datetime-local'){
194 if(!empty($meta_value)){
195 if(strpos($meta_value, '-') !== FALSE){
196 }else{
197 $meta_value = date('Y-m-d H:i', $meta_value);
198 }
199 $meta_value = str_replace(' ', 'T', $meta_value);
200 }
201 JetCustomTableExport::$export_instance->data[$id][ $meta_key ] = $meta_value;
202 }
203 elseif($field_type == 'repeater'){
204 foreach ($get_cpt_fields as $row) {
205 foreach ($row as $meta_key => $meta_value) {
206 if (!isset($field_types[$meta_key])) continue;
207
208 $field_type = $field_types[$meta_key];
209
210 if ($field_type === 'repeater') {
211 $unser = @unserialize($meta_value);
212 if (!is_array($unser)) continue;
213
214 $collected_values = [];
215
216 foreach ($unser as $idkey => $array) {
217 if (!empty($array)) {
218 foreach ($array as $array_key => $array_val) {
219 if (isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'text') {
220 $collected_values[$array_key][] = $array_val;
221 }
222 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'checkbox') {
223 // foreach($array_val as $arrval){
224 $exp_value = [];
225
226 foreach($array_val as $key => $metvalue){
227 if($metvalue == 'true'){
228 $exp_value[] = $key;
229
230 }
231
232 }
233 $collected_values[$array_key][] = implode(',',$exp_value );
234 }
235 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'media') {
236 $medias = [];
237 if(is_numeric($array_val)){
238 $medias[] = wp_get_attachment_url($array_val);
239 }
240 elseif(is_array($array_val)){
241 $medias[] = $array_val['url'];
242
243 }
244 else{
245 $medias[]=$array_val;
246 }
247 $collected_values[$array_key][] = implode('|',$medias );
248 }
249 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'gallery'){
250 $galleries =[];
251
252 if(is_array($array_val)){
253 foreach($array_val as $key => $gallryvalue){
254 $galleries[] = $gallryvalue['url'];
255 }
256
257 }
258 else{
259 $gallery= explode(',',$array_val);
260 foreach($gallery as $gallerykey => $galleryval){
261 if(is_numeric($galleryval)){
262 $galleries[] = wp_get_attachment_url($galleryval);
263 }
264 else{
265 $galleries[]=$galleryval;
266 }
267
268
269 }
270 }
271 $collected_values[$array_key][] = implode(',',$galleries );
272 }
273 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'posts'){
274 $test =[];
275 if(is_array($array_val)){
276
277 foreach($array_val as $postkey => $metpostvalue){
278 if(is_numeric($metpostvalue)){
279 $title = $wpdb->get_row($wpdb->prepare("select post_title from {$wpdb->prefix}posts where ID = %d ORDER BY ID DESC",$metpostvalue));
280 $test[] = $title->post_title;
281
282 }
283
284 }
285 $collected_values[$array_key][] = implode(',',$test );
286
287 }
288 else{
289
290 if(is_numeric($array_val)){
291 $title = $wpdb->get_row($wpdb->prepare("select post_title from {$wpdb->prefix}posts where ID = %d ORDER BY ID DESC",$array_val));
292 $testing = $title->post_title;
293 }
294 $collected_values[$array_key][] = $testing;
295 }
296 }
297 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'select'){
298 if(is_array($array_val)){
299 $select =[];
300 foreach($array_val as $metselectvalue){
301 $select[] = $metselectvalue;
302 $collected_values[$array_key][] = implode(',',$select );
303 }
304 }
305 else{
306 $collected_values[$array_key][]= $array_val;
307 }
308 }
309 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'date'){
310 $repdateval = array();
311 if(!empty($array_val)){
312 $repdateval[] = $array_val;
313 }else{
314 $repdateval[] = "";
315 }
316 if(!empty($repdateval)){
317 $collected_values[$array_key][] = implode('|',$repdateval);
318 }
319 }
320 else if(isset($jet_rep_cpttypes[$array_key]) && $jet_rep_cpttypes[$array_key] === 'datetime-local'){
321 $repdateval = array();
322 if(!empty($array_val)){
323 $timestamp = strtotime($array_val); // Convert to Unix timestamp
324 $arrval = date('Y-m-d H:i', $timestamp); // Format the date properly
325
326 $repdateval[] = $arrval;
327 }else{
328 $repdateval[] = "";
329 }
330 if(!empty($repdateval)){
331 $collected_values[$array_key][] = implode('|',$repdateval);
332 }
333 }
334 // Process other fields
335 }
336 }
337 }
338 foreach ($collected_values as $key => $values) {
339 JetCustomTableExport::$export_instance->data[$id][$key] = implode('|', $values);
340 }
341 }
342 }
343 }
344
345 }
346 else if (is_object($meta_value) && isset($meta_value)) {
347 // Check if $meta_value is a JSON string
348 if (is_string($meta_value) && json_decode($meta_value)) {
349 $meta_value = json_decode($meta_value, true);
350 }
351
352 $is_unserialized = is_array($meta_value);
353
354 if ($is_unserialized) {
355 $output_array = [];
356
357 foreach ($meta_value as $key => $val) {
358 // If the value is an array (like 'week_days'), use '|' as a separator
359 if (is_array($val)) {
360 $output_array[] = implode('|', $val); // Use '|' for arrays
361 } else {
362 // Otherwise, just add the value as is
363 $output_array[] = $val;
364 }
365 }
366
367 // Join values with commas for CSV format
368 $value_all = implode(',', $output_array);
369
370 JetCustomTableExport::$export_instance->data[$id][$meta_key] = $value_all;
371 }
372 }
373 else {
374 JetCustomTableExport::$export_instance->data[$id][$meta_key] = $meta_value; // Default case
375 }
376 }
377 }
378 }
379 }
380 }