PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.1
Import WP – CSV & XML Import Export for WordPress v2.7.1
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Parser / AbstractParser.php

AbstractParser.php in Import WP – CSV & XML Import Export for WordPress 2.7.1, at class/Common/Importer/Parser/AbstractParser.php

122 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\Parser;
4
5 use ImportWP\Common\Importer\FileInterface;
6
7 abstract class AbstractParser
8 {
9 /**
10 * @var \ImportWP\Common\Importer\FileInterface $file
11 */
12 protected $file;
13
14 protected $record_index;
15 protected $record;
16
17 /**
18 * Parser constructor.
19 *
20 * @param \ImportWP\Common\Importer\FileInterface $file
21 */
22 public function __construct(FileInterface $file)
23 {
24 $this->file = $file;
25 }
26
27 public function getRecord($record_index = 0)
28 {
29
30 if ($this->record_index !== $record_index) {
31 $this->record_index = $record_index;
32 $this->record = $this->file->getRecord($this->getRecordIndex());
33 $this->onRecordLoaded();
34 }
35
36 return $this;
37 }
38
39 public function getRecordIndex()
40 {
41 return $this->record_index;
42 }
43
44 abstract protected function onRecordLoaded();
45
46 public function queryGroup($group)
47 {
48 $output = [];
49
50 if (isset($group['fields'])) {
51 foreach ($group['fields'] as $field_key => $field_value) {
52 $output[$field_key] = $this->query_string($field_value);
53 }
54 }
55
56 return $output;
57 }
58
59 /**
60 * Parse Query String for {} run query on them
61 *
62 * @param string $query
63 *
64 * @return string
65 */
66 public function query_string($query)
67 {
68
69 $output = preg_replace_callback('/{(.*?)}/', array($this, 'query_matches'), $query);
70 $output = $this->handle_custom_methods($output);
71
72 return $output;
73 }
74
75 public function query_matches($matches)
76 {
77 if (isset($matches[0]) && isset($matches[1])) {
78 return $this->query($matches[1]);
79 }
80
81 return '';
82 }
83
84 abstract public function query($query);
85
86 public function file()
87 {
88 return $this->file;
89 }
90
91 public function handle_custom_methods($input)
92 {
93 // m: Multiline modifier
94 // s: matches all characters including newlines
95 // U: non-greedy matching
96 $input = preg_replace_callback('/\[([\w]+)\((.*?)\)\]/msU', function ($matches) {
97
98 $method = $matches[1];
99
100 $result = [];
101 $args = [];
102
103 // Dont split comma's if they are inside a double quote
104 if (preg_match_all('/(?:".*?"|[^",\s]+)(?=\s*,|\s*$)/s', $matches[2], $result) > 0) {
105 $args = $result[0];
106 foreach ($args as &$arg) {
107
108 // Strip commas from start and end of string
109 $arg = preg_replace('/^(\'(.*)\'|"(.*)")$/s', '$2$3', $arg);
110 }
111 }
112
113 if (is_callable($method)) {
114 return call_user_func_array($method, $args);
115 }
116
117 return $matches[0];
118 }, $input);
119 return $input;
120 }
121 }
122