PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.3.6
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.3.6
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / src / App / Controller / GoogleCategoriesController.php
wp-all-export / src / App / Controller Last commit date
CategoriesController.php 8 years ago ExportController.php 4 years ago GoogleCategoriesController.php 8 years ago SchedulingConnectionController.php 8 years ago SchedulingLicenseController.php 7 years ago
GoogleCategoriesController.php
132 lines
1 <?php
2
3 namespace Wpae\App\Controller;
4
5 use Wpae\Controller\BaseController;
6 use Wpae\Http\JsonResponse;
7 use Wpae\Http\Request;
8
9 class GoogleCategoriesController extends BaseController
10 {
11 public function getAction(Request $request)
12 {
13 global $wpdb;
14
15 $tablePrefix = $this->getTablePrefix();
16
17 $response = array();
18
19 $search = $request->get('search', '');
20 $parent = $request->get('parent');
21
22 $searchString = '';
23
24 if(!is_null($parent)) {
25 $searchString .= $wpdb->prepare(" AND `parent_id` = %d ", $parent);
26 }
27
28 if($search) {
29 $searchString = $wpdb->prepare(" AND `name` LIKE %s LIMIT 50", '%'.$wpdb->esc_like($search).'%');
30 }
31
32 $querystr = "SELECT * FROM `{$tablePrefix}google_cats` WHERE 1=1 $searchString";
33 $pageposts = $wpdb->get_results($querystr, ARRAY_A);
34
35 // If it's a search find the parents of the categories
36 if($search) {
37 $parents = [];
38
39 foreach($pageposts as $category) {
40
41 if(!$category['parent_id']) {
42 $parents = array_merge($parents, [$category]);
43 }
44
45 $sql = "SELECT * FROM `{$tablePrefix}google_cats` WHERE `id` = $category[parent_id]";
46 $results = $wpdb->get_results($sql, ARRAY_A);
47
48 foreach ($results as &$result) {
49 $result['children'] = [$this->processCategory($category, $search)];
50 }
51
52 $parents = array_merge($parents, $results);
53 }
54
55 $pageposts = $parents;
56 }
57
58 foreach($pageposts as $category) {
59 $catItem = $this->processCategory($category, $search);
60 $response[] = $catItem;
61 }
62
63 if(!$parent) {
64 $response = array('name' => 'Root', 'children' => $response);
65 }
66
67 return new JsonResponse($response);
68 }
69
70 /**
71 * @param $categoryId
72 * @return mixed
73 * @internal param $category
74 * @internal param $wpdb
75 */
76 private function categoryHasChildren($categoryId)
77 {
78 global $wpdb;
79
80 $tablePrefix = $this->getTablePrefix();
81
82 $categoryId = intval($categoryId);
83
84 $childrenQuerystr = "SELECT COUNT(*) as hasChildren FROM `{$tablePrefix}google_cats` WHERE `parent_id` = %d";
85 $childrenQuerystr = $wpdb->prepare($childrenQuerystr, $categoryId);
86 $hasChildren = $wpdb->get_results($childrenQuerystr, ARRAY_A);
87 $hasChildren = $hasChildren[0]['hasChildren'];
88 return $hasChildren;
89 }
90
91 /**
92 * @param $category
93 * @param $search
94 * @return array
95 */
96 private function processCategory($category, $search)
97 {
98 //TODO: Optimize this and prepare statements
99 $hasChildren = $this->categoryHasChildren($category['id']);
100 if ($search) {
101 $categoryName = preg_replace("/".preg_quote($search)."/i", "<b>\$0</b>", $category['name']);
102 } else {
103 $categoryName = $category['name'];
104 }
105
106 $catItem = array(
107 'name' => $categoryName,
108 'hasChildren' => $hasChildren,
109 'parentName' => $category['parentName'],
110 'id' => $category['id'],
111 'opened' => false,
112 'visible' => true
113 );
114
115 if (isset($category['children'])) {
116 $catItem['children'] = $category['children'];
117 $catItem['opened'] = true;
118 return $catItem;
119 }
120 return $catItem;
121 }
122
123 /**
124 * @return string
125 */
126 private function getTablePrefix()
127 {
128 $plugin = \PMXE_Plugin::getInstance();
129 $tablePrefix = $plugin->getTablePrefix();
130 return $tablePrefix;
131 }
132 }