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 / WPQueryExport.php

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

640 lines 29.0 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
8 namespace Smackcoders\SMEXP;
9 if ( ! defined( 'ABSPATH' ) )
10 exit; // Exit if accessed directly
11 // require_once dirname(__FILE__) . '/ExportExtension.php';
12 // require_once dirname(__FILE__) . '/PostExport.php';
13 // require_once dirname(__FILE__) . '/WooComExport.php';
14 // require_once dirname(__FILE__) . '/LearnPress.php';
15
16 // use Smackcoders\SMEXP\ExportExtension;
17 // use Smackcoders\SMEXP\PostExport;
18 // use Smackcoders\SMEXP\WooCommerceExport;
19 // use Smackcoders\SMEXP\LearnPressExport;
20
21
22 class WPQueryExport extends ExportExtension{
23 // class WPQueryExport {
24
25 protected static $instance = null,$export_instance,$post_export,$woocom_export,$learnpress_export;
26 public static function getInstance() {
27 if ( null == self::$instance ) {
28 self::$instance = new self;
29 WPQueryExport::$export_instance = ExportExtension::getInstance();
30 WPQueryExport::$post_export = PostExport::getInstance();
31 WPQueryExport::$woocom_export = WooCommerceExport::getInstance();
32 WPQueryExport::$learnpress_export = LearnPressExport::getInstance();
33
34 }
35 return self::$instance;
36 }
37
38 /**
39 * CustomerReviewExport constructor.
40 */
41 public function __construct() {
42 add_action('wp_ajax_wpquery_data', [$this,'wpquery_data_function']);
43 add_action('wp_ajax_nopriv_wpquery_data', [$this, 'wpquery_data_function']); // If guests can access it
44 // $this->plugin = Plugin::getInstance();
45
46 }
47
48 public function wpquery_data_function(){
49
50 if (!is_user_logged_in() || !current_user_can('manage_options')) {
51 wp_send_json_error(['message' => 'Unauthorized access.'], 403);
52 return;
53 }
54
55 $query_data = sanitize_text_field($_POST['query_data']);
56 $type = sanitize_text_field($_POST['type']);
57 $query_args = str_replace(['\\"', ' '], ['"', ''], $query_data);
58
59 // Step 2: Split by comma to get key-value pairs
60 // $pairs = explode(',', $query_args);
61
62 $args = [];
63
64 // foreach ($pairs as $pair) {
65 // // Step 3: Split each pair by =>
66 // list($key, $value) = explode('=>', $pair);
67
68 // // Step 4: Trim quotes
69 // $key = trim($key, '"');
70 // $value = trim($value, '"');
71
72 // // Step 5: Add to array
73 // $args[$key] = $value;
74 // }
75
76 // $args = array($query_data)
77 $step1 = str_replace('=>', ':', $query_args);
78 $step2 = str_replace(
79 ['array(', ')', "'",],
80 ['[', ']', '"'],
81 $step1
82 );
83 $json_string = '{' . $step2 . '}';
84 $json_string = preg_replace('/,\s*\]/', ']', $json_string); // remove trailing commas
85 $args = json_decode(stripslashes($json_string), true);
86 $result = $this->validate_wpai_query_args($args,$type);
87
88 if(isset($result->errors)){
89 wp_send_json_error(['message' => 'Error']);
90 }
91 else{
92 $total_row =0;
93 if($type == 'post'){
94 if(!array_key_exists('posts_per_page',$args)){
95 $args['posts_per_page'] = -1;
96 }
97 $query = new \WP_Query($args);
98 $posts = $query->get_posts();
99 $total_row = count($posts);
100 $module = $posts[0]->post_type;
101 if($module == 'product' && is_plugin_active('woocommerce/woocommerce.php')){
102 $module = 'WooCommerce';
103 $optionalType = '';
104 }
105 elseif($module == "shop_coupon" && is_plugin_active('woocommerce/woocommerce.php')){
106 $module = 'WooCommerceCoupons';
107 $optionalType = '';
108 }
109 elseif($module == "shop_order_placehold" && is_plugin_active('woocommerce/woocommerce.php')){
110 $module = 'WooCommerceOrders';
111 $optionalType = '';
112 }
113 else{
114 if($module !== 'post' && $module !=='page'){
115 $optionalType = $module;
116 $module = 'CustomPosts';
117
118 }
119 if($module == 'post'){
120 $module = 'Posts';
121 }
122 if($module=='page'){
123 $module= 'Pages';
124 }
125 }
126 }
127 elseif($type == "user"){
128 if(!array_key_exists('number',$args)){
129 $args['number'] = -1;
130 }
131 $user_query = new \WP_User_Query($args);
132 $users = $user_query->get_results();
133 $module = 'Users';
134 if(!empty($users)){
135 $total_row = count($users);
136 }
137
138 }
139 elseif($type == "comment"){
140 if(!array_key_exists('number',$args)){
141 // $args['number'] = -1;
142 // $args['cache_results'] = false;
143 }
144 $comment_query = new \WP_Comment_Query($args);
145 $comments = $comment_query->comments;
146 if(!empty($comments)){
147 $total_row = count($comments);
148 }
149 $module = 'Comments';
150 }
151 wp_send_json_success(['message' => 'success','total_row' => $total_row ." ".$module]);
152 }
153 $query = new \WP_Query($query_args);
154 }
155 public function validate_wpai_query_args($args,$type) {
156 if (($type == 'post') && (!is_array($args) || !isset($args['post_type']))) {
157 return new \WP_Error('invalid_query', 'Invalid WP_Query arguments.');
158 }
159 elseif($type == 'user' && (!is_array($args) || !isset($args['role']))){
160 return new \WP_Error('invalid_query', 'Invalid WP_Query arguments.');
161 }
162 elseif($type == 'comment' && (!is_array($args) || !isset($args['status']))){
163 return new \WP_Error('invalid_query', 'Invalid WP_Query arguments.');
164 }
165 return $args;
166 }
167 public function exportwpquery($query_args){
168
169
170 // $query_args = json_decode(stripslashes($query_args), true);
171 // $query_args = str_replace(['\\"', ' '], ['"', ''], $query_args);
172 // $pairs = explode(',', $query_args);
173 // $args = [];
174 // foreach ($pairs as $pair) {
175 // list($key, $value) = explode('=>', $pair);
176 // $key = trim($key, '"');
177 // $value = trim($value, '"');
178 // $args[$key] = $value;
179 // }
180 $step1 = str_replace('=>', ':', $query_args);
181 $step2 = str_replace(
182 ['array(', ')', "'",],
183 ['[', ']', '"'],
184 $step1
185 );
186 $json_string = '{' . $step2 . '}';
187 $json_string = preg_replace('/,\s*\]/', ']', $json_string); // remove trailing commas
188 $args = json_decode(stripslashes($json_string), true);
189 // $converted = str_replace('=>', ':', $query_args);
190
191
192 // $converted = preg_replace('/array\s*\(/', '{', $converted);
193 // $converted = preg_replace('/\)/', '}', $converted);
194
195
196 // $converted = preg_replace('/"([^"]*?)"\s*:/', '"$1":', $converted); // keys
197 // $converted = preg_replace('/: \s*"([^"]*?)"/', ': "$1"', $converted); // values
198
199
200 // $converted = preg_replace('/,\s*}/', '}', $converted);
201 // $converted = preg_replace('/,\s*]/', ']', $converted);
202
203
204
205 // $json_like = '{' . $converted . '}';
206
207
208 // $args = json_decode($json_like, true);
209 // $patterns = [
210 // '/array\s*\(/' => '[',
211 // '/\)/' => ']',
212 // '/=>/' => ':',
213 // ];
214
215
216 // $jsonLike = preg_replace(array_keys($patterns), array_values($patterns), $query_args);
217
218
219 // $jsonString = '{' . $jsonLike . '}';
220
221
222 // $jsonString = preg_replace('/"(\w+)"\s*:/', '"$1":', $jsonString);
223 // $jsonString = preg_replace('/:\s*"([^"]+)"/', ': "$1"', $jsonString);
224
225 // $args = json_decode($jsonString, true);
226 if(!array_key_exists('posts_per_page',$args)){
227 $args['posts_per_page'] = -1;
228 }
229 $query = new \WP_Query($args);
230 $posts = $query->get_posts();
231 WPQueryExport::$export_instance = ExportExtension::getInstance();
232 WPQueryExport::$post_export = PostExport::getInstance();
233 WPQueryExport::$woocom_export = WooCommerceExport::getInstance();
234 WPQueryExport::$learnpress_export = LearnPressExport::getInstance();
235 if(!empty($posts)){
236 WPQueryExport::$export_instance->totalRowCount = count($posts);
237 }
238 $offset = WPQueryExport::$export_instance->offset;
239 $limit = WPQueryExport::$export_instance->limit;
240 foreach($posts as $post_key => $post_val){
241 $post_meta_data = get_post_meta($post_val->ID);
242
243 $postId = $post_val->ID;
244 $module = $post_val->post_type;
245 if($module == 'product' && is_plugin_active('woocommerce/woocommerce.php')){
246 $module = 'WooCommerce';
247 $optionalType = '';
248 }
249 else{
250 if($module !== 'post' && $module !=='page'){
251 $optionalType = $module;
252 $module = 'CustomPosts';
253
254 }
255 if($module == 'post'){
256 $module = 'Posts';
257 }
258 if($module=='page'){
259 $module= 'Pages';
260 }
261 }
262
263 $postids [] = $post_val->ID;
264 }
265 WPQueryExport::$export_instance->generateHeaders($module, $this->optionalType);
266
267
268 $post_ids = !empty($postids) ? array_slice($postids, $offset, $limit) : [];
269
270
271 foreach($post_ids as $postkey => $postId){
272 // foreach($post_val as $postkey => $postval){
273
274 // WPQueryExport::$export_instance->data[$post_val->ID][$postkey] = $postval;
275 if ($module == 'Posts' || $module == 'WooCommerce' || $module == 'CustomPosts' || $module == 'Categories' || $exp_module == 'Tags' || $exp_module == 'Taxonomies' || $exp_module == 'Pages')
276 {
277 WPQueryExport::$export_instance->getWPMLData($postId, $this->optionalType, $module);
278 }
279
280 if ($module == 'Posts' || $module == 'CustomPosts' || $module == 'Pages' || $module == 'WooCommerce')
281 {
282 if(is_plugin_active('polylang/polylang.php') || is_plugin_active('polylang-pro/polylang.php') || is_plugin_active('polylang-wc/polylang-wc.php')){
283 WPQueryExport::$export_instance->getPolylangData($postId, $this->optionalType, $exp_module);
284 }
285 }
286 WPQueryExport::$export_instance->data[$postId] = WPQueryExport::$export_instance->getPostsDataBasedOnRecordId($postId,$module);
287 WPQueryExport::$post_export->getPostsMetaDataBasedOnRecordId($postId, $module, $optionalType);
288 WPQueryExport::$export_instance->getTermsAndTaxonomies($postId, $module, $optionalType);
289 if ($module == 'WooCommerce') WPQueryExport::$woocom_export->getProductData($postId, $module, $optionalType);
290 // if ($module == 'WooCommerceRefunds') ExportExtension::$woocom_export->getWooComCustomerUser($postId, $this->module, $this->optionalType);
291 if ($module == 'WooCommerceOrders') WPQueryExport::$woocom_export->getWooComOrderData($postId, $this->module, $this->optionalType);
292 if ($module == 'WooCommerceVariations') WPQueryExport::$woocom_export->getVariationData($postId, $this->module, $this->optionalType);
293 if($module == 'WooCommerceCoupons') WPQueryExport::$woocom_export->getCouponsData($postId, $this->module, $this->optionalType);
294 if ($optionalType == 'lp_course') WPQueryExport::$learnpress_export->getCourseData($postId);
295 if ($optionalType == 'lp_lesson') WPQueryExport::$learnpress_export->getLessonData($postId);
296 if ($optionalType == 'lp_quiz') WPQueryExport::$learnpress_export->getQuizData($postId);
297 if ($optionalType == 'lp_question') WPQueryExport::$learnpress_export->getQuestionData($postId);
298 if ($optionalType == 'lp_order') WPQueryExport::$learnpress_export->getOrderData($postId);
299
300 if ($optionalType == 'stm-courses') WPQueryExport::$woocom_export->getCourseDataMasterLMS($postId);
301
302 if ($optionalType == 'stm-questions') WPQueryExport::$woocom_export->getQuestionDataMasterLMS($postId);
303
304 if ($optionalType == 'stm-lessons') WPQueryExport::$woocom_export->getLessonDataMasterLMS($postId);
305 if ($optionalType == 'stm-orders') WPQueryExport::$woocom_export->orderDataMasterLMS($postId);
306 if ($optionalType == 'stm-quizzes') WPQueryExport::$woocom_export->quizzDataMasterLMS($postId);
307 if ($optionalType == 'elementor_library') WPQueryExport::$woocom_export->elementor_export($postId);
308 if ($optionalType == 'nav_menu_item') WPQueryExport::$woocom_export->getMenuData($postId);
309
310 // if ($optionalType == 'widgets') self::$instance->getWidgetData($postId, $this->headers);
311 // }
312 }
313 /** Added post format for 'standard' property */
314 if ($module == 'Posts' || $module == 'CustomPosts' || $module == 'WooCommerce')
315 {
316 foreach (WPQueryExport::$export_instance->data as $id => $records)
317 {
318 if (!array_key_exists('post_format', $records))
319 {
320 $records['post_format'] = 'standard';
321
322 WPQueryExport::$export_instance->data[$id] = $records;
323 }
324 }
325
326 }
327 if($optionalType == 'course'){
328 foreach(WPQueryExport::$export_instance->data as $id => $records){
329 if(array_key_exists('_llms_instructors',$records)){
330 $instructor=unserialize($records['_llms_instructors']);
331 if(is_array($instructor)){
332 $arr_ins=array();
333 foreach($instructor as $ins_val){
334 $arr_val=array_values($ins_val);
335 unset($arr_val[0]);
336 unset($arr_val[2]);
337 $arr_ins[] = implode(',',$arr_val);
338
339 }
340 $records['_llms_instructors'] = implode('|',$arr_ins);
341 WPQueryExport::$export_instance->data[$id] = $records;
342
343 }
344
345
346 }
347 }
348 }
349
350
351 $result = WPQueryExport::$export_instance->finalDataToExport(WPQueryExport::$export_instance->data, $module,$optionalType);
352 WPQueryExport::$export_instance->proceedExport($result);
353
354
355 }
356 public function exportwpquery_user($query_args){
357
358 // $query_args = str_replace(['\\"', ' '], ['"', ''], $query_args);
359 // $pairs = explode(',', $query_args);
360 // $args = [];
361 // foreach ($pairs as $pair) {
362 // list($key, $value) = explode('=>', $pair);
363 // $key = trim($key, '"');
364 // $value = trim($value, '"');
365 // $args[$key] = $value;
366 // }
367 $step1 = str_replace('=>', ':', $query_args);
368 $step2 = str_replace(
369 ['array(', ')', "'",],
370 ['[', ']', '"'],
371 $step1
372 );
373 $json_string = '{' . $step2 . '}';
374 $json_string = preg_replace('/,\s*\]/', ']', $json_string);
375 $args = json_decode(stripslashes($json_string), true);
376 if(!array_key_exists('number',$args)){
377 $args['number'] = -1;
378 }
379 $user_query = new \WP_User_Query($args);
380 $users = $user_query->get_results();
381 $module = 'Users';
382
383 WPQueryExport::$export_instance = ExportExtension::getInstance();
384 WPQueryExport::$post_export = PostExport::getInstance();
385 WPQueryExport::$export_instance->generateHeaders($module, $this->optionalType);
386
387 $offset = WPQueryExport::$export_instance->offset;
388 $limit = WPQueryExport::$export_instance->limit;
389 if(is_countable($users)&& !empty($users)){
390 WPQueryExport::$export_instance->totalRowCount = count($users);
391 $userids = array();
392 foreach($users as $user_key => $user_val){
393 $userids [] = $user_val->ID;
394 $user_data = $user_val->data;
395 foreach($user_data as $userkey => $userval){
396 WPQueryExport::$export_instance->data[$user_val->ID][$userkey] = $userval;
397 }
398 }
399 $user_ids = !empty($users) ? array_slice($userids, $offset, $limit) : [];
400
401 }
402 global $wpdb;
403 foreach($user_ids as $userkey => $userId){
404 $userMeta = $wpdb->get_results($wpdb->prepare("SELECT user_id, meta_key, meta_value FROM {$wpdb->prefix}users wp JOIN {$wpdb->prefix}usermeta wpm ON wpm.user_id = wp.ID WHERE ID = %d", absint($userId)));
405 $wptypesfields = get_option('wpcf-usermeta');
406 $wptypesfields = get_option('wpcf-usermeta');
407
408 if (!empty($wptypesfields))
409 {
410 $i = 1;
411 foreach ($wptypesfields as $key => $value)
412 {
413 $typesf[$i] = 'wpcf-' . $key;
414 $typeOftypesField[$typesf[$i]] = $value['type'];
415 $i++;
416 }
417 }
418 if (!empty($userMeta))
419 {
420 foreach ($userMeta as $userMetaInfo)
421 {
422 if ($userMetaInfo->meta_key == $wpdb->prefix.'capabilities')
423 {
424
425 if(is_plugin_active('members/members.php')){
426 $data = unserialize($userMetaInfo->meta_value);
427 $roles = array_keys(array_filter($data));
428 $role = implode('|', $roles);
429 WPQueryExport::$export_instance->data[ $userId ][ 'multi_user_role' ] = $role;
430 }
431 else{
432 $userRole = WPQueryExport::$export_instance->getUserRole($userMetaInfo->meta_value);
433 WPQueryExport::$export_instance->data[ $userId ][ 'role' ] = $userRole;
434 }
435
436 }
437 elseif ($userMetaInfo->meta_key == 'description')
438 {
439 WPQueryExport::$export_instance->data[$userId]['biographical_info'] = $userMetaInfo->meta_value;
440 }
441 elseif ($userMetaInfo->meta_key == 'comment_shortcuts')
442 {
443 WPQueryExport::$export_instance->data[$userId]['enable_keyboard_shortcuts'] = $userMetaInfo->meta_value;
444 }
445 elseif ($userMetaInfo->meta_key == 'show_admin_bar_front')
446 {
447 WPQueryExport::$export_instance->data[$userId]['show_toolbar'] = $userMetaInfo->meta_value;
448 }
449 elseif ($userMetaInfo->meta_key == 'rich_editing')
450 {
451 WPQueryExport::$export_instance->data[$userId]['disable_visual_editor'] = $userMetaInfo->meta_value;
452 }
453 elseif ($userMetaInfo->meta_key == 'locale')
454 {
455 WPQueryExport::$export_instance->data[$userId]['language'] = $userMetaInfo->meta_value;
456 }
457 elseif (isset($typesf) && in_array($userMetaInfo->meta_key, $typesf))
458 {
459 $typeoftype = $typeOftypesField[$userMetaInfo->meta_key];
460 if (is_serialized($userMetaInfo->meta_value))
461 {
462 $typefileds = unserialize($userMetaInfo->meta_value);
463 $typedata = "";
464 foreach ($typefileds as $key2 => $value2)
465 {
466 if (is_array($value2))
467 {
468 foreach ($value2 as $key3 => $value3)
469 {
470 $typedata .= $value3 . ',';
471 }
472 }
473 else $typedata .= $value2 . ',';
474 }
475 if (preg_match('/wpcf-/', $userMetaInfo->meta_key))
476 {
477 $userMetaInfo->meta_key = preg_replace('/wpcf-/', '', $userMetaInfo->meta_key);
478 WPQueryExport::$export_instance->data[$userId][$userMetaInfo->meta_key] = substr($typedata, 0, -1);
479 }
480 }
481 elseif ($typeoftype == 'date')
482 {
483 WPQueryExport::$export_instance->data[$userId][$userMetaInfo->meta_key] = date('Y-m-d', $userMetaInfo->meta_value);
484 }
485 $multi_row = '_' . $userMetaInfo->meta_key . '-sort-order';
486
487 $multi_data = get_user_meta($userId, $multi_row);
488 $multi_data = $multi_data[0];
489 if (is_array($multi_data))
490 {
491 foreach ($multi_data as $k => $mid)
492 {
493 $m_data = WPQueryExport::$export_instance->get_common_post_metadata($mid);
494 if ($typeoftype == 'date') $multi_data[$k] = date('Y-m-d H:i:s', $m_data['meta_value']);
495 else $multi_data[$k] = $m_data['meta_value'];
496 }
497 WPQueryExport::$export_instance->data[$userId][$userMetaInfo->meta_key] = implode('|', $multi_data);
498 if (preg_match('/wpcf-/', $userMetaInfo->meta_key))
499 {
500 $userMetaInfo->meta_key = preg_replace('/wpcf-/', '', $userMetaInfo->meta_key);
501
502 WPQueryExport::$export_instance->data[$userId][$userMetaInfo->meta_key] = implode('|', $multi_data);
503 }
504 }
505 else
506 {
507 if (preg_match('/wpcf-/', $userMetaInfo->meta_key))
508 {
509 $userMetaInfo->meta_key = preg_replace('/wpcf-/', '', $userMetaInfo->meta_key);
510 WPQueryExport::$export_instance->data[$userId][$userMetaInfo->meta_key] = $userMetaInfo->meta_value;
511 }
512 }
513 }
514
515 else
516 {
517
518 WPQueryExport::$export_instance->data[$userId][$userMetaInfo
519 ->meta_key] = $userMetaInfo->meta_value;
520 }
521 }
522 // Prepare the buddy meta details to be export
523 if (is_plugin_active('buddypress/bp-loader.php'))
524 {
525 $query_to_fetch_buddy_meta = $wpdb->prepare("SELECT user_id,field_id,value,name FROM {$wpdb->prefix}bp_xprofile_data bxd inner join {$wpdb->prefix}users wp on bxd.user_id = wp.ID inner join {$wpdb->prefix}bp_xprofile_fields bxf on bxf.id = bxd.field_id where user_id=%d", $userId);
526 $buddy = $wpdb->get_results($query_to_fetch_buddy_meta);
527 if (!empty($buddy))
528 {
529 foreach ($buddy as $buddyInfo)
530 {
531 foreach ($buddyInfo as $field_id => $value)
532 {
533 $this->data[$userId][$buddyInfo
534 ->name] = $buddyInfo->value;
535 }
536 }
537 }
538 }
539 WPQueryExport::$post_export->getPostsMetaDataBasedOnRecordId($userId, $module, $this->optionalType);
540 }
541 }
542 $result = WPQueryExport::$export_instance->finalDataToExport(WPQueryExport::$export_instance->data, $module,$this->optionalType);
543 WPQueryExport::$export_instance->proceedExport($result);
544 }
545 public function exportwpquery_comment($query_args){
546
547 // $query_args = json_decode(stripslashes($args), true);
548 // $query_args = str_replace(['\\"', ' '], ['"', ''], $query_args);
549 // $pairs = explode(',', $query_args);
550 // $args = [];
551 // foreach ($pairs as $pair) {
552 // // Step 3: Split each pair by =>
553 // list($key, $value) = explode('=>', $pair);
554 // // Step 4: Trim quotes
555 // $key = trim($key, '"');
556 // $value = trim($value, '"');
557 // // Step 5: Add to array
558 // $args[$key] = $value;
559 // }
560 $step1 = str_replace('=>', ':', $query_args);
561 $step2 = str_replace(
562 ['array(', ')', "'",],
563 ['[', ']', '"'],
564 $step1
565 );
566 $json_string = '{' . $step2 . '}';
567 $json_string = preg_replace('/,\s*\]/', ']', $json_string); // remove trailing commas
568 $args = json_decode(stripslashes($json_string), true);
569 if(!array_key_exists('number',$args)){
570 // $args['number'] = -1;
571 // $args['cache_results'] = false;
572 }
573
574 $comment_query = new \WP_Comment_Query($args);
575 $comments = $comment_query->comments;
576 $module = 'Comments';
577 WPQueryExport::$export_instance = ExportExtension::getInstance();
578 WPQueryExport::$post_export = PostExport::getInstance();
579 WPQueryExport::$export_instance->generateHeaders($module, $this->optionalType);
580
581 $offset = WPQueryExport::$export_instance->offset;
582 $limit = WPQueryExport::$export_instance->limit;
583 if(is_countable($comments)){
584 WPQueryExport::$export_instance->totalRowCount = count($comments);
585 $commentids = array();
586 foreach($comments as $comment_key => $comment_val){
587 $commentids [] = $comment_val->comment_ID;
588 foreach($comment_val as $commentkey => $commentval){
589 WPQueryExport::$export_instance->data[$comment_val->comment_ID][$commentkey] = $commentval;
590 }
591
592 }
593 $comment_ids = !empty($comments) ? array_slice($commentids, $offset, $limit) : [];
594 foreach($comment_ids as $comment_key => $comment_id){
595 foreach($comments as $comment_key => $comment_val){
596 if($comment_id == $comment_val->comment_ID){
597 $limited_comments = $comment_val;
598 if (!empty($limited_comments))
599 {
600 foreach ($limited_comments as $commentInfo)
601 {
602 $user_id = $commentInfo->user_id;
603 if (!empty($user_id))
604 {
605 $users_login = $wpdb->get_results($wpdb->prepare("SELECT user_login FROM {$wpdb->prefix}users WHERE ID = %d", absint($user_id)));
606 foreach ($users_login as $users_key => $users_value)
607 {
608 foreach ($users_value as $u_key => $u_value)
609 {
610 $users_id = $u_value;
611 }
612 }
613 }
614 foreach ($commentInfo as $commentKey => $commentVal)
615 {
616 WPQueryExport::$export_instance->data[$commentInfo->comment_ID][$commentKey] = $commentVal;
617 WPQueryExport::$export_instance->data[$commentInfo->comment_ID]['user_id'] = isset($users_id) ? $users_id : '';
618 }
619 $get_comment_rating = get_comment_meta($commentInfo->comment_ID, 'rating', true);
620 if(!empty($get_comment_rating)){
621 WPQueryExport::$export_instance->data[$commentInfo->comment_ID]['comment_rating'] = $get_comment_rating;
622 }
623 }
624 }
625 }
626 }
627
628 }
629
630 $result = WPQueryExport::$export_instance->finalDataToExport(WPQueryExport::$export_instance->data, $module,$this->optionalType);
631 if ($mode == null) WPQueryExport::$export_instance->proceedExport($result);
632 else return $result;
633
634 }
635 }
636 }
637
638 global $wpquery_exp_class;
639 $wpquery_exp_class = new WPQueryExport();
640