PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.0.10
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.0.10
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Controllers / BoxImport.php

BoxImport.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 2.0.10, at Inc/Core/Controllers/BoxImport.php

216 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 2.0.10 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2023 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Controllers;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Base\Form;
20
21 class BoxImport extends BaseController
22 {
23 /**
24 * The form settings name
25 *
26 * @var string
27 */
28 const settings_name = 'firebox_import';
29
30 /**
31 * Render the page content
32 *
33 * @return void
34 */
35 public function render()
36 {
37 // page content
38 add_action('firebox/settings_page', [$this, 'settingsPageContent']);
39
40 // render layout
41 firebox()->renderer->admin->render('pages/settings');
42 }
43
44 /**
45 * Import box
46 *
47 * @param array $input
48 *
49 * @return void
50 */
51 public function processBoxesImport($input)
52 {
53 $file = $this->getUploadedFile();
54
55 // ensure a file was given
56 if (!is_array($file) || !isset($file['name']) || empty($file['name']))
57 {
58 add_settings_error(self::settings_name, 'settings_updated', fpframework()->_('FPF_PLEASE_SELECT_A_FILE_TO_UPLOAD'), 'error');
59 return;
60 }
61
62 $ext = explode('.', $file['name']);
63
64 // ensure given file plugin was given
65 if (!in_array($ext[count($ext) - 1], ['fbox']))
66 {
67 add_settings_error(self::settings_name, 'settings_updated', fpframework()->_('FPF_PLEASE_CHOOSE_A_VALID_FILE'), 'error');
68 return;
69 }
70
71 $publish_all = isset($input['publish_all']) ? $input['publish_all'] : 0;
72
73 // read file contents
74 $data = $this->getUploadedFileContents($file['tmp_name']);
75
76 // if empty data file then abort
77 if (empty($data))
78 {
79 add_settings_error(self::settings_name, 'settings_updated', fpframework()->_('FPF_FILE_EMPTY'), 'error');
80 return;
81 }
82
83 if (!$items = json_decode($data, true))
84 {
85 add_settings_error(self::settings_name, 'settings_updated', firebox()->_('FB_POPUP_IMPORT_CONTENTS_ERROR'), 'error');
86 return;
87 }
88
89 if (is_null($items))
90 {
91 $items = [];
92 }
93
94 if (!$items)
95 {
96 return;
97 }
98
99 // import all boxes
100 if (!$new_box_id = $this->importBoxes($items, $publish_all))
101 {
102 add_settings_error(self::settings_name, 'settings_updated', firebox()->_('FB_POPUP_IMPORT_CONTENTS_ERROR'), 'error');
103 return;
104 }
105
106 add_settings_error(self::settings_name, 'settings_updated', fpframework()->_('FPF_ITEMS_SAVED'), 'success');
107 return $new_box_id;
108 }
109
110 /**
111 * Returns the uploaded file
112 *
113 * @return mixed
114 */
115 protected function getUploadedFile()
116 {
117 $file = isset($_FILES['file']) ? $_FILES['file'] : '';
118 return $file;
119 }
120
121 /**
122 * Returns the contents of the file
123 *
124 * @param string $tmp_name
125 *
126 * @return string
127 */
128 protected function getUploadedFileContents($tmp_name)
129 {
130 return file_get_contents($tmp_name);
131 }
132
133 /**
134 * Imports boxes data
135 *
136 * @param array $items
137 * @param int $publish_all
138 *
139 * @return boolean
140 */
141 protected function importBoxes($items, $publish_all = 0)
142 {
143 $success = true;
144
145 foreach ($items as $item)
146 {
147 if (!isset($item['meta']))
148 {
149 $success = false;
150 break;
151 }
152
153 // get meta
154 $meta = $item['meta'];
155
156 // remote meta from item
157 unset($item['meta']);
158
159 if (!isset($item['box']))
160 {
161 $success = false;
162 break;
163 }
164
165 $box = $item['box'];
166
167 // remove ID
168 $box['ID'] = '';
169
170 $factory = new \FPFramework\Base\Factory();
171
172 $tz = wp_timezone();
173 $date_without_tz = $factory->getDate();
174 $date_with_tz = $factory->getDate()->setTimezone($tz);
175
176 $box['post_date'] = $date_with_tz->format('Y-m-d H:i:s');
177 $box['post_date_gmt'] = $date_without_tz->format('Y-m-d H:i:s');
178
179 // set publish status
180 if (in_array($publish_all, [0, 1]))
181 {
182 $box['post_status'] = ($publish_all == 0) ? 'draft' : 'publish';
183 }
184
185 // insert new box
186 if (!$new_box_id = firebox()->tables->box->insert((object) $box))
187 {
188 $success = false;
189 break;
190 }
191
192 // add meta options for new box
193 update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta));
194 $success = $new_box_id;
195 }
196
197 return $success;
198 }
199
200 /**
201 * What the settings page will contain
202 *
203 * @return void
204 */
205 public function settingsPageContent()
206 {
207 $form = new Form(\FireBox\Core\Admin\Forms\Import::getSettings(), [
208 'fields_name_prefix' => self::settings_name,
209 'section_name' => self::settings_name,
210 'class' => 'settings-ui-inner-fields',
211 'button_label' => 'FPF_IMPORT'
212 ]);
213
214 echo $form->render();
215 }
216 }