PageRenderTime 75ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-all-import-pro/actions/wp_loaded.php

https://gitlab.com/najomie/fit-hippie
PHP | 281 lines | 208 code | 57 blank | 16 comment | 27 complexity | 1320637a4c4ebfa10d63dceb802c24af MD5 | raw file
  1. <?php
  2. function pmxi_wp_loaded() {
  3. $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'imports';
  4. global $wpdb;
  5. $imports = $wpdb->get_results("SELECT `id`, `name`, `path` FROM $table WHERE `path` IS NULL", ARRAY_A);
  6. if ( ! empty($imports) ){
  7. $importRecord = new PMXI_Import_Record();
  8. $importRecord->clear();
  9. foreach ($imports as $imp) {
  10. $importRecord->getById($imp['id']);
  11. if ( ! $importRecord->isEmpty()){
  12. $importRecord->delete( true );
  13. }
  14. $importRecord->clear();
  15. }
  16. }
  17. /* Check if cron is manualy, then execute import */
  18. $cron_job_key = PMXI_Plugin::getInstance()->getOption('cron_job_key');
  19. if (!empty($cron_job_key) and !empty($_GET['import_key']) and $_GET['import_key'] == $cron_job_key and !empty($_GET['action']) and in_array($_GET['action'], array('processing','trigger','pipe','cancel','cleanup'))) {
  20. $logger = create_function('$m', 'echo "<p>$m</p>\\n";');
  21. if (empty($_GET['import_id']))
  22. {
  23. if ($_GET['action'] == 'cleanup')
  24. {
  25. $settings = new PMXI_Admin_Settings();
  26. $settings->cleanup( true );
  27. wp_send_json(array(
  28. 'status' => 200,
  29. 'message' => __('Cleanup completed.', 'wp_all_import_plugin')
  30. ));
  31. return;
  32. }
  33. wp_send_json(array(
  34. 'status' => 403,
  35. 'message' => __('Missing import ID.', 'wp_all_import_plugin')
  36. ));
  37. return;
  38. }
  39. $import = new PMXI_Import_Record();
  40. $ids = explode(',', $_GET['import_id']);
  41. if (!empty($ids) and is_array($ids)){
  42. foreach ($ids as $id) { if (empty($id)) continue;
  43. $import->getById($id);
  44. if ( ! $import->isEmpty() ){
  45. if ( ! empty($_GET['sync']) )
  46. {
  47. $imports = $wpdb->get_results("SELECT `id`, `name`, `path` FROM $table WHERE `processing` = 1", ARRAY_A);
  48. if ( ! empty($imports) )
  49. {
  50. $processing_ids = array();
  51. foreach ($imports as $imp) {
  52. $processing_ids[] = $imp['id'];
  53. }
  54. wp_send_json(array(
  55. 'status' => 403,
  56. 'message' => sprintf(__('Other imports are currently in process [%s].', 'wp_all_import_plugin'), implode(",", $processing_ids))
  57. ));
  58. //$logger and call_user_func($logger, sprintf(__('Other imports are currently in process [%s].', 'wp_all_import_plugin'), implode(",", $processing_ids)));
  59. break;
  60. }
  61. }
  62. if ( ! in_array($import->type, array('url', 'ftp', 'file')) ) {
  63. wp_send_json(array(
  64. 'status' => 500,
  65. 'message' => sprintf(__('Scheduling update is not working with "upload" import type. Import #%s.', 'wp_all_import_plugin'), $id)
  66. ));
  67. //$logger and call_user_func($logger, sprintf(__('Scheduling update is not working with "upload" import type. Import #%s.', 'wp_all_import_plugin'), $id));
  68. }
  69. switch ($_GET['action']) {
  70. case 'trigger':
  71. if ( (int) $import->executing ){
  72. wp_send_json(array(
  73. 'status' => 403,
  74. 'message' => sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id)
  75. ));
  76. //$logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id));
  77. }
  78. elseif ( ! $import->processing and ! $import->triggered ){
  79. $import->set(array(
  80. 'triggered' => 1,
  81. 'imported' => 0,
  82. 'created' => 0,
  83. 'updated' => 0,
  84. 'skipped' => 0,
  85. 'deleted' => 0,
  86. 'queue_chunk_number' => 0,
  87. 'last_activity' => date('Y-m-d H:i:s')
  88. ))->update();
  89. $history_log = new PMXI_History_Record();
  90. $history_log->set(array(
  91. 'import_id' => $import->id,
  92. 'date' => date('Y-m-d H:i:s'),
  93. 'type' => 'trigger',
  94. 'summary' => __("triggered by cron", "wp_all_import_plugin")
  95. ))->save();
  96. //$logger and call_user_func($logger, sprintf(__('#%s Cron job triggered.', 'wp_all_import_plugin'), $id));
  97. wp_send_json(array(
  98. 'status' => 200,
  99. 'message' => sprintf(__('#%s Cron job triggered.', 'wp_all_import_plugin'), $id)
  100. ));
  101. }
  102. elseif( $import->processing and ! $import->triggered) {
  103. wp_send_json(array(
  104. 'status' => 403,
  105. 'message' => sprintf(__('Import #%s currently in process. Request skipped.', 'wp_all_import_plugin'), $id)
  106. ));
  107. //$logger and call_user_func($logger, sprintf(__('Import #%s currently in process. Request skipped.', 'wp_all_import_plugin'), $id));
  108. }
  109. elseif( ! $import->processing and $import->triggered){
  110. wp_send_json(array(
  111. 'status' => 403,
  112. 'message' => sprintf(__('Import #%s already triggered. Request skipped.', 'wp_all_import_plugin'), $id)
  113. ));
  114. //$logger and call_user_func($logger, sprintf(__('Import #%s already triggered. Request skipped.', 'wp_all_import_plugin'), $id));
  115. }
  116. break;
  117. case 'processing':
  118. if ( $import->processing == 1 and (time() - strtotime($import->registered_on)) > ((PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit')) ? PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') : 120)){ // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally.
  119. $import->set(array(
  120. 'processing' => 0
  121. ))->update();
  122. }
  123. // start execution imports that is in the cron process
  124. if ( ! (int) $import->triggered ){
  125. wp_send_json(array(
  126. 'status' => 403,
  127. 'message' => sprintf(__('Import #%s is not triggered. Request skipped.', 'wp_all_import_plugin'), $id)
  128. ));
  129. //$logger and call_user_func($logger, sprintf(__('Import #%s is not triggered. Request skipped.', 'wp_all_import_plugin'), $id));
  130. }
  131. elseif ( (int) $import->executing ){
  132. wp_send_json(array(
  133. 'status' => 403,
  134. 'message' => sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id)
  135. ));
  136. //$logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id));
  137. }
  138. elseif ( (int) $import->triggered and ! (int) $import->processing ){
  139. $log_storage = (int) PMXI_Plugin::getInstance()->getOption('log_storage');
  140. // unlink previous logs
  141. $by = array();
  142. $by[] = array(array('import_id' => $id, 'type NOT LIKE' => 'trigger'), 'AND');
  143. $historyLogs = new PMXI_History_List();
  144. $historyLogs->setColumns('id', 'import_id', 'type', 'date')->getBy($by, 'id ASC');
  145. if ($historyLogs->count() and $historyLogs->count() >= $log_storage ){
  146. $logsToRemove = $historyLogs->count() - $log_storage;
  147. foreach ($historyLogs as $i => $file){
  148. $historyRecord = new PMXI_History_Record();
  149. $historyRecord->getBy('id', $file['id']);
  150. if ( ! $historyRecord->isEmpty()) $historyRecord->delete(); // unlink history file only
  151. if ($i == $logsToRemove)
  152. break;
  153. }
  154. }
  155. $history_log = new PMXI_History_Record();
  156. $history_log->set(array(
  157. 'import_id' => $import->id,
  158. 'date' => date('Y-m-d H:i:s'),
  159. 'type' => 'processing',
  160. 'summary' => __("cron processing", "wp_all_import_plugin")
  161. ))->save();
  162. if ($log_storage){
  163. $wp_uploads = wp_upload_dir();
  164. $log_file = wp_all_import_secure_file( $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::LOGS_DIRECTORY, $history_log->id ) . DIRECTORY_SEPARATOR . $history_log->id . '.html';
  165. if ( @file_exists($log_file) ) wp_all_import_remove_source($log_file, false);
  166. //@file_put_contents($log_file, sprintf(__('<p>Source path `%s`</p>', 'wp_all_import_plugin'), $import->path));
  167. }
  168. ob_start();
  169. $response = $import->set(array('canceled' => 0, 'failed' => 0))->execute($logger, true, $history_log->id);
  170. $log_data = ob_get_clean();
  171. if ($log_storage){
  172. $log = @fopen($log_file, 'a+');
  173. @fwrite($log, $log_data);
  174. @fclose($log);
  175. }
  176. if ( ! empty($response) and is_array($response)){
  177. wp_send_json($response);
  178. }
  179. elseif ( ! (int) $import->queue_chunk_number ){
  180. wp_send_json(array(
  181. 'status' => 200,
  182. 'message' => sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $import->id)
  183. ));
  184. //$logger and call_user_func($logger, sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $import->id));
  185. }
  186. else{
  187. wp_send_json(array(
  188. 'status' => 200,
  189. 'message' => sprintf(__('Records Processed %s. Records Count %s.', 'wp_all_import_plugin'), (int) $import->queue_chunk_number, (int) $import->count)
  190. ));
  191. // $logger and call_user_func($logger, sprintf(__('Records Count %s', 'wp_all_import_plugin'), (int) $import->count));
  192. // $logger and call_user_func($logger, sprintf(__('Records Processed %s', 'wp_all_import_plugin'), (int) $import->queue_chunk_number));
  193. }
  194. }
  195. else {
  196. wp_send_json(array(
  197. 'status' => 403,
  198. 'message' => sprintf(__('Import #%s already processing. Request skipped.', 'wp_all_import_plugin'), $id)
  199. ));
  200. //$logger and call_user_func($logger, sprintf(__('Import #%s already processing. Request skipped.', 'wp_all_import_plugin'), $id));
  201. }
  202. break;
  203. case 'pipe':
  204. $import->execute($logger);
  205. break;
  206. case 'cancel':
  207. $import->set(array(
  208. 'triggered' => 0,
  209. 'processing' => 0,
  210. 'executing' => 0,
  211. 'canceled' => 1,
  212. 'canceled_on' => date('Y-m-d H:i:s')
  213. ))->update();
  214. wp_send_json(array(
  215. 'status' => 200,
  216. 'message' => sprintf(__('Import #%s canceled', 'wp_all_import_plugin'), $import->id)
  217. ));
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }