Server IP : 172.67.157.199 / Your IP : 3.128.205.122 [ Web Server : Apache System : Linux b70eb322-3aee-0c53-7c82-0db91281f2c6.secureserver.net 6.1.90-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 2 12:09:22 EDT 2024 x86_64 User : root ( 0) PHP Version : 8.0.30.2 Disable Function : NONE Domains : 0 Domains MySQL : ON | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/wp-content/plugins/wp-all-export/src/App/Controller/ |
Upload File : |
<?php namespace Wpae\App\Controller; use Wpae\Controller\BaseController; use Wpae\Http\JsonResponse; use Wpae\Http\Request; class GoogleCategoriesController extends BaseController { public function getAction(Request $request) { global $wpdb; $tablePrefix = $this->getTablePrefix(); $response = array(); $search = $request->get('search', ''); $parent = $request->get('parent'); $searchString = ''; if(!is_null($parent)) { $searchString .= $wpdb->prepare(" AND `parent_id` = %d ", $parent); } if($search) { $searchString = $wpdb->prepare(" AND `name` LIKE %s LIMIT 50", '%'.$wpdb->esc_like($search).'%'); } $querystr = "SELECT * FROM `{$tablePrefix}google_cats` WHERE 1=1 $searchString"; $pageposts = $wpdb->get_results($querystr, ARRAY_A); // If it's a search find the parents of the categories if($search) { $parents = []; foreach($pageposts as $category) { if(!$category['parent_id']) { $parents = array_merge($parents, [$category]); } $sql = "SELECT * FROM `{$tablePrefix}google_cats` WHERE `id` = $category[parent_id]"; $results = $wpdb->get_results($sql, ARRAY_A); foreach ($results as &$result) { $result['children'] = [$this->processCategory($category, $search)]; } $parents = array_merge($parents, $results); } $pageposts = $parents; } foreach($pageposts as $category) { $catItem = $this->processCategory($category, $search); $response[] = $catItem; } if(!$parent) { $response = array('name' => 'Root', 'children' => $response); } return new JsonResponse($response); } /** * @param $categoryId * @return mixed * @internal param $category * @internal param $wpdb */ private function categoryHasChildren($categoryId) { global $wpdb; $tablePrefix = $this->getTablePrefix(); $categoryId = intval($categoryId); $childrenQuerystr = "SELECT COUNT(*) as hasChildren FROM `{$tablePrefix}google_cats` WHERE `parent_id` = %d"; $childrenQuerystr = $wpdb->prepare($childrenQuerystr, $categoryId); $hasChildren = $wpdb->get_results($childrenQuerystr, ARRAY_A); $hasChildren = $hasChildren[0]['hasChildren']; return $hasChildren; } /** * @param $category * @param $search * @return array */ private function processCategory($category, $search) { //TODO: Optimize this and prepare statements $hasChildren = $this->categoryHasChildren($category['id']); if ($search) { $categoryName = preg_replace("/".preg_quote($search)."/i", "<b>\$0</b>", $category['name']); } else { $categoryName = $category['name']; } $catItem = array( 'name' => $categoryName, 'hasChildren' => $hasChildren, 'parentName' => $category['parentName'], 'id' => $category['id'], 'opened' => false, 'visible' => true ); if (isset($category['children'])) { $catItem['children'] = $category['children']; $catItem['opened'] = true; return $catItem; } return $catItem; } /** * @return string */ private function getTablePrefix() { $plugin = \PMXE_Plugin::getInstance(); $tablePrefix = $plugin->getTablePrefix(); return $tablePrefix; } }