PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / wizard / sampledata.php
vikappointments / libraries / wizard Last commit date
sampledata.php 1 month ago shortcodes.php 1 month ago
sampledata.php
240 lines
1 <?php
2 /**
3 * @package VikAppointments - Libraries
4 * @subpackage wizard
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Implement the wizard step used to download and
16 * install a sample data package.
17 *
18 * @since 1.2.3
19 */
20 class VAPWizardStepSampleData extends VAPWizardStep
21 {
22 /**
23 * Returns the step title.
24 * Used as a very-short description.
25 *
26 * @return string The step title.
27 */
28 public function getTitle()
29 {
30 return __('Sample Data', 'vikappointments');
31 }
32
33 /**
34 * Returns the step description.
35 *
36 * @return string The step description.
37 */
38 public function getDescription()
39 {
40 return __('<p>It is possible to download here a set of sample data, which will auto-populate VikAppointments to be immediately ready to use.</p>', 'vikappointments');
41 }
42
43 /**
44 * Returns an optional step icon.
45 *
46 * @return string The step icon.
47 */
48 public function getIcon()
49 {
50 return '<i class="fas fa-download"></i>';
51 }
52
53 /**
54 * Return the group to which the step belongs.
55 *
56 * @return string The group name.
57 */
58 public function getGroup()
59 {
60 // belongs to GLOBAL group
61 return JText::translate('VAPMENUTITLEHEADER3');
62 }
63
64 /**
65 * Returns the HTML to display description and actions
66 * needed to complete the step.
67 *
68 * @return string The HTML of the step.
69 */
70 public function display()
71 {
72 // always try to search for a layout related to this step
73 return JLayoutHelper::render('html.wizard.sampledata', array('step' => $this));
74 }
75
76 /**
77 * Checks whether the specified step can be skipped.
78 * By default, all the steps are mandatory.
79 *
80 * @return boolean True if skippable, false otherwise.
81 */
82 public function canIgnore()
83 {
84 return true;
85 }
86
87 /**
88 * Implements the step execution.
89 *
90 * @param JRegistry $data The request data.
91 *
92 * @return boolean
93 */
94 protected function doExecute($data)
95 {
96 // get selected sample data
97 $id = $data->get('sampledata');
98
99 JLoader::import('adapter.filesystem.folder');
100
101 // get temporary dir
102 $tmp = get_temp_dir();
103
104 // clean temporary path
105 $tmp = rtrim(JPath::clean($tmp), DIRECTORY_SEPARATOR);
106
107 // make sure the folder exists
108 if (!is_dir($tmp))
109 {
110 throw new Exception(sprintf('Temporary folder [%s] does not exist', $tmp), 404);
111 }
112
113 // make sure the temporary folder is writable
114 if (!wp_is_writable($tmp))
115 {
116 throw new Exception(sprintf('Temporary folder [%s] is not writable', $tmp), 403);
117 }
118
119 // download end-point
120 $url = 'https://vikwp.com/api/?task=sampledata.download';
121
122 // init HTTP transport
123 $http = new JHttp();
124
125 // build smaple data file name
126 $packname = 'sampledata-' . uniqid();
127
128 // build request headers
129 $headers = array(
130 // turn on stream to push body within a file
131 'stream' => true,
132 // define the filepath in which the data will be pushed
133 'filename' => $tmp . DIRECTORY_SEPARATOR . $packname . '.zip',
134 // make sure the request is non blocking
135 'blocking' => true,
136 // force timeout to 60 seconds
137 'timeout' => 60,
138 );
139
140 // build post data
141 $data = array(
142 'id' => $id,
143 );
144
145 // make connection with VikWP server
146 $response = $http->post($url, $data, $headers);
147
148 if ($response->code != 200)
149 {
150 // raise error returned by VikWP
151 throw new Exception($response->body, $response->code);
152 }
153
154 // make sure the file has been saved
155 if (!JFile::exists($headers['filename']))
156 {
157 throw new Exception('ZIP package could not be saved on disk', 404);
158 }
159
160 // get backup model
161 $model = JModelVAP::getInstance('backup');
162
163 $error = null;
164
165 // attempt to install the sample data as a backup
166 if (!$model->restore($headers['filename']))
167 {
168 // get last registered error
169 $error = $model->getError();
170
171 if (!$error instanceof Exception)
172 {
173 // create an exception with the fetched error message
174 $error = new Exception($error, 500);
175 }
176 }
177
178 // always delete the downloaded package
179 JFile::delete($headers['filename']);
180
181 if ($error)
182 {
183 // propagate error
184 throw $error;
185 }
186
187 return true;
188 }
189
190 /**
191 * Returns a list of supported sample data.
192 *
193 * @return array A list of sample data.
194 */
195 public function getSampleData()
196 {
197 // build transient key
198 $transient = 'vikappointments_sampledata_' . md5(VIKAPPOINTMENTS_SOFTWARE_VERSION);
199
200 // get cached sample data list
201 $data = get_transient($transient);
202
203 if ($data)
204 {
205 // return cached transient
206 return json_decode($data);
207 }
208
209 // instantiate HTTP transport
210 $http = new JHttp();
211
212 // build end-point URI
213 $uri = 'https://vikwp.com/api/?task=sampledata.list';
214
215 // build post data
216 $post = array(
217 'sku' => 'vap',
218 'version' => VIKAPPOINTMENTS_SOFTWARE_VERSION,
219 );
220
221 // load sample data from server
222 $response = $http->post($uri, $post);
223
224 if ($response->code == 200)
225 {
226 // decode response
227 $data = json_decode($response->body);
228
229 // cache sample data list for an hour
230 set_transient($transient, json_encode($data), HOUR_IN_SECONDS);
231 }
232 else
233 {
234 $data = array();
235 }
236
237 return $data;
238 }
239 }
240