PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-lister-for-ebay/classes/WPL_AjaxHandler.php

https://bitbucket.org/sanders_nick/my-maxi-skirt
PHP | 452 lines | 285 code | 104 blank | 63 comment | 20 complexity | 8a92bfd722de9c0828cff647698cc379 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. class WPL_AjaxHandler extends WPL_Core {
  3. public function __construct() {
  4. parent::__construct();
  5. // called from category tree
  6. add_action('wp_ajax_e2e_get_ebay_categories_tree', array( &$this, 'ajax_get_ebay_categories_tree' ) );
  7. add_action('wp_ajax_e2e_get_store_categories_tree', array( &$this, 'ajax_get_store_categories_tree' ) );
  8. // called from edit products page
  9. add_action('wp_ajax_wpl_getCategorySpecifics', array( &$this, 'ajax_getCategorySpecifics' ) );
  10. // called from jobs window
  11. add_action('wp_ajax_wpl_jobs_load_tasks', array( &$this, 'jobs_load_tasks' ) );
  12. add_action('wp_ajax_wpl_jobs_run_task', array( &$this, 'jobs_run_task' ) );
  13. add_action('wp_ajax_wpl_jobs_complete_job', array( &$this, 'jobs_complete_job' ) );
  14. // handle incoming ebay notifications
  15. add_action('wp_ajax_handle_ebay_notify', array( &$this, 'ajax_handle_ebay_notify' ) );
  16. add_action('wp_ajax_nopriv_handle_ebay_notify', array( &$this, 'ajax_handle_ebay_notify' ) );
  17. }
  18. // fetch category specifics
  19. public function ajax_getCategorySpecifics() {
  20. $category_id = $_REQUEST['id'];
  21. $this->initEC();
  22. $result = $this->EC->getCategorySpecifics( $category_id );
  23. $this->EC->closeEbay();
  24. $this->returnJSON( $result );
  25. exit();
  26. }
  27. // run single task
  28. public function jobs_run_task() {
  29. // quit if no job name provided
  30. if ( ! isset( $_REQUEST['job'] ) ) return false;
  31. if ( ! isset( $_REQUEST['task'] ) ) return false;
  32. $job = $_REQUEST['job'];
  33. $task = $_REQUEST['task'];
  34. $this->logger->info('running task: '.print_r($task,1));
  35. // handle job name
  36. switch ( $task['task'] ) {
  37. case 'loadShippingServices':
  38. // call EbayController
  39. $this->initEC();
  40. $result = $this->EC->loadShippingServices();
  41. $this->EC->closeEbay();
  42. // build response
  43. $response = new stdClass();
  44. $response->job = $job;
  45. $response->task = $task;
  46. $response->result = $result;
  47. $response->errors = array();
  48. $response->success = true;
  49. $this->returnJSON( $response );
  50. exit();
  51. case 'loadPaymentOptions':
  52. // call EbayController
  53. $this->initEC();
  54. $result = $this->EC->loadPaymentOptions();
  55. $this->EC->closeEbay();
  56. // build response
  57. $response = new stdClass();
  58. $response->job = $job;
  59. $response->task = $task;
  60. $response->result = $result;
  61. $response->errors = array();
  62. $response->success = true;
  63. $this->returnJSON( $response );
  64. exit();
  65. case 'loadStoreCategories':
  66. // call EbayController
  67. $this->initEC();
  68. $result = $this->EC->loadStoreCategories();
  69. $this->EC->closeEbay();
  70. // build response
  71. $response = new stdClass();
  72. $response->job = $job;
  73. $response->task = $task;
  74. $response->result = $result;
  75. $response->errors = array();
  76. $response->success = true;
  77. $this->returnJSON( $response );
  78. exit();
  79. case 'loadEbayCategoriesBranch':
  80. // call EbayController
  81. $this->initEC();
  82. $result = $this->EC->loadEbayCategoriesBranch( $task['cat_id'] );
  83. $this->EC->closeEbay();
  84. // build response
  85. $response = new stdClass();
  86. $response->job = $job;
  87. $response->task = $task;
  88. $response->result = $result;
  89. $response->errors = array();
  90. $response->success = true;
  91. $this->returnJSON( $response );
  92. exit();
  93. case 'verifyItem':
  94. // call EbayController
  95. $this->initEC();
  96. $results = $this->EC->verifyItems( $task['id'] );
  97. $this->EC->closeEbay();
  98. // build response
  99. $response = new stdClass();
  100. $response->job = $job;
  101. $response->task = $task;
  102. $response->errors = $results[0]->errors;
  103. $response->success = $results[0]->success;
  104. $this->returnJSON( $response );
  105. exit();
  106. case 'publishItem':
  107. // call EbayController
  108. $this->initEC();
  109. $results = $this->EC->sendItemsToEbay( $task['id'] );
  110. $this->EC->closeEbay();
  111. // build response
  112. $response = new stdClass();
  113. $response->job = $job;
  114. $response->task = $task;
  115. $response->errors = $results[0]->errors;
  116. $response->success = $results[0]->success;
  117. $this->returnJSON( $response );
  118. exit();
  119. case 'reviseItem':
  120. // call EbayController
  121. $this->initEC();
  122. $results = $this->EC->reviseItems( $task['id'] );
  123. $this->EC->closeEbay();
  124. // build response
  125. $response = new stdClass();
  126. $response->job = $job;
  127. $response->task = $task;
  128. $response->errors = $results[0]->errors;
  129. $response->success = $results[0]->success;
  130. $this->returnJSON( $response );
  131. exit();
  132. case 'updateItem':
  133. // call EbayController
  134. $this->initEC();
  135. $results = $this->EC->updateItemsFromEbay( $task['id'] );
  136. $this->EC->closeEbay();
  137. // build response
  138. $response = new stdClass();
  139. $response->job = $job;
  140. $response->task = $task;
  141. $response->errors = $results[0]->errors;
  142. $response->success = $results[0]->success;
  143. $this->returnJSON( $response );
  144. exit();
  145. default:
  146. // echo "unknown task";
  147. // exit();
  148. }
  149. }
  150. // load task list
  151. public function jobs_load_tasks() {
  152. // quit if no job name provided
  153. if ( ! isset( $_REQUEST['job'] ) ) return false;
  154. $jobname = $_REQUEST['job'];
  155. // handle job name
  156. switch ( $jobname ) {
  157. case 'updateEbayData':
  158. // call EbayController
  159. $this->initEC();
  160. $tasks = $this->EC->initCategoriesUpdate();
  161. $this->EC->closeEbay();
  162. // build response
  163. $response = new stdClass();
  164. $response->tasklist = $tasks;
  165. $response->total_tasks = count( $tasks );
  166. $response->error = '';
  167. $response->success = true;
  168. // create new job
  169. $newJob = new stdClass();
  170. $newJob->jobname = $jobname;
  171. $newJob->tasklist = $tasks;
  172. $job = new JobsModel( $newJob );
  173. $response->job_key = $job->key;
  174. $this->returnJSON( $response );
  175. exit();
  176. case 'verifyAllPreparedItems':
  177. // get prepared items
  178. $sm = new ListingsModel();
  179. $items = $sm->getAllPrepared();
  180. // create job from items and send response
  181. $response = $this->_create_bulk_listing_job( 'verifyItem', $items, $jobname );
  182. $this->returnJSON( $response );
  183. exit();
  184. case 'publishAllVerifiedItems':
  185. // get verified items
  186. $sm = new ListingsModel();
  187. $items = $sm->getAllVerified();
  188. // create job from items and send response
  189. $response = $this->_create_bulk_listing_job( 'publishItem', $items, $jobname );
  190. $this->returnJSON( $response );
  191. exit();
  192. case 'reviseAllChangedItems':
  193. // get changed items
  194. $sm = new ListingsModel();
  195. $items = $sm->getAllChanged();
  196. // create job from items and send response
  197. $response = $this->_create_bulk_listing_job( 'reviseItem', $items, $jobname );
  198. $this->returnJSON( $response );
  199. exit();
  200. case 'updateAllPublishedItems':
  201. // get published items
  202. $sm = new ListingsModel();
  203. $items = $sm->getAllPublished();
  204. // create job from items and send response
  205. $response = $this->_create_bulk_listing_job( 'updateItem', $items, $jobname );
  206. $this->returnJSON( $response );
  207. exit();
  208. default:
  209. // echo "unknown job";
  210. // break;
  211. }
  212. // exit();
  213. }
  214. // create bulk listing job
  215. public function _create_bulk_listing_job( $taskname, $items, $jobname ) {
  216. // create tasklist
  217. $tasks = array();
  218. foreach( $items as $item ) {
  219. $this->logger->info('adding task for item #'.$item['id'] . ' - '.$item['auction_title']);
  220. $task = array(
  221. 'task' => $taskname,
  222. 'displayName' => $item['auction_title'],
  223. 'id' => $item['id']
  224. );
  225. $tasks[] = $task;
  226. }
  227. // build response
  228. $response = new stdClass();
  229. $response->tasklist = $tasks;
  230. $response->total_tasks = count( $tasks );
  231. $response->error = '';
  232. $response->success = true;
  233. // create new job
  234. $newJob = new stdClass();
  235. $newJob->jobname = $jobname;
  236. $newJob->tasklist = $tasks;
  237. $job = new JobsModel( $newJob );
  238. $response->job_key = $job->key;
  239. return $response;
  240. }
  241. // load task list
  242. public function jobs_complete_job() {
  243. // quit if no job name provided
  244. if ( ! isset( $_REQUEST['job'] ) ) return false;
  245. // mark job as completed
  246. $job = new JobsModel( $_REQUEST['job'] );
  247. $job->completeJob();
  248. if ( 'updateEbayData' == $job->item['job_name'] ) {
  249. // if we were updating ebay details as part of setup, move to next step
  250. if ( '2' == self::getOption('setup_next_step') ) self::updateOption('setup_next_step', 3);
  251. }
  252. // build response
  253. $response = new stdClass();
  254. $response->msg = $job->item['job_name'].' comleted';
  255. $response->error = '';
  256. $response->success = true;
  257. $response->job_key = $job->key;
  258. $this->returnJSON( $response );
  259. exit();
  260. }
  261. public function returnJSON( $data ) {
  262. header('content-type: application/json; charset=utf-8');
  263. echo json_encode( $data );
  264. }
  265. // get categories tree node - used on ProfilesPage
  266. public function ajax_get_ebay_categories_tree() {
  267. $path = $_POST["dir"];
  268. $parent_cat_id = basename( $path );
  269. $categories = EbayCategoriesModel::getChildrenOf( $parent_cat_id );
  270. if( count($categories) > 0 ) {
  271. echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
  272. // All dirs
  273. foreach( $categories as $cat ) {
  274. if ( $cat['leaf'] == '0' ) {
  275. echo '<li class="directory collapsed"><a href="#" rel="'
  276. . ($_POST['dir'] . $cat['cat_id']) . '/">'. ($cat['cat_name']) . '</a></li>';
  277. }
  278. }
  279. // All files
  280. foreach( $categories as $cat ) {
  281. if ( $cat['leaf'] == '1' ) {
  282. $ext = 'txt';
  283. echo '<li class="file ext_txt"><a href="#" rel="'
  284. . ($_POST['dir'] . $cat['cat_id']) . '">' . ($cat['cat_name']) . '</a></li>';
  285. }
  286. }
  287. echo "</ul>";
  288. }
  289. exit();
  290. }
  291. // get categories tree node - used on ProfilesPage
  292. public function ajax_get_store_categories_tree() {
  293. $path = $_POST["dir"];
  294. $parent_cat_id = basename( $path );
  295. $categories = EbayCategoriesModel::getChildrenOfStoreCategory( $parent_cat_id );
  296. if( count($categories) > 0 ) {
  297. echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
  298. // All dirs
  299. foreach( $categories as $cat ) {
  300. if ( $cat['leaf'] == '0' ) {
  301. echo '<li class="directory collapsed"><a href="#" rel="'
  302. . ($_POST['dir'] . $cat['cat_id']) . '/">'. ($cat['cat_name']) . '</a></li>';
  303. }
  304. }
  305. // All files
  306. foreach( $categories as $cat ) {
  307. if ( $cat['leaf'] == '1' ) {
  308. $ext = 'txt';
  309. echo '<li class="file ext_txt"><a href="#" rel="'
  310. . ($_POST['dir'] . $cat['cat_id']) . '">' . ($cat['cat_name']) . '</a></li>';
  311. }
  312. }
  313. echo "</ul>";
  314. }
  315. exit();
  316. }
  317. // there are still problems with eBay's notification system.
  318. // this handler is for debugging purposes - it will send request details to the developer
  319. // for manual test call: www.example.com/wp-admin/admin-ajax.php?action=handle_ebay_notify
  320. public function ajax_handle_ebay_notify() {
  321. require_once 'EbatNs_NotificationClient.php';
  322. require_once 'EbatNs_ResponseError.php';
  323. $handler = new EbatNs_NotificationClient();
  324. $body = file_get_contents('php://input');
  325. $res = $handler->getResponse($body);
  326. $this->logger->info('handle_ebay_notify() - time: '.date('Y-m-d H:i:s') );
  327. #$this->logger->info('POST:'.print_r($_POST,1));
  328. $this->logger->info('REQUEST:'.print_r($_REQUEST,1));
  329. $this->logger->info('SERVER:'.print_r($_SERVER,1));
  330. $headers = getallheaders();
  331. $this->logger->info('headers:'.print_r($headers,1));
  332. $this->logger->info('body:'.print_r($body,1));
  333. $this->logger->info('response:'.print_r($res,1));
  334. $msg = 'I received a notification at '.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n\n";
  335. $msg .= 'Body: '.print_r($body,1)."\n\n";
  336. $msg .= 'Response: '.print_r($res,1)."\n";
  337. $msg .= 'REQUEST: '.print_r($_REQUEST,1)."\n";
  338. $msg .= 'SERVER: '.print_r($_SERVER,1)."\n";
  339. $msg .= 'Headers: '.print_r($headers,1)."\n";
  340. $to = get_option('admin_email', 'support@wplab.com');
  341. $subject = 'New eBay platform notification';
  342. wp_mail($to, $subject, $msg);
  343. echo 'OK';
  344. exit();
  345. }
  346. }
  347. // instantiate object
  348. $oWPL_AjaxHandler = new WPL_AjaxHandler();