PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.2
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.2
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 1.0.2, at Inc/Core/Controllers/BoxImport.php

209 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 1.0.2 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2021 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 // import all boxes
95 if (!$new_box_id = $this->importBoxes($items, $publish_all))
96 {
97 add_settings_error( self::settings_name, 'settings_updated', firebox()->_('FB_POPUP_IMPORT_CONTENTS_ERROR'), 'error');
98 return;
99 }
100
101 add_settings_error( self::settings_name, 'settings_updated', fpframework()->_('FPF_ITEMS_SAVED'), 'success');
102 return $new_box_id;
103 }
104
105 /**
106 * Returns the uploaded file
107 *
108 * @return mixed
109 */
110 protected function getUploadedFile()
111 {
112 $file = isset($_FILES['file']) ? $_FILES['file'] : '';
113 return $file;
114 }
115
116 /**
117 * Returns the contents of the file
118 *
119 * @param string $tmp_name
120 *
121 * @return string
122 */
123 protected function getUploadedFileContents($tmp_name)
124 {
125 return file_get_contents($tmp_name);
126 }
127
128 /**
129 * Imports boxes data
130 *
131 * @param array $items
132 * @param int $publish_all
133 *
134 * @return boolean
135 */
136 protected function importBoxes($items, $publish_all = 0)
137 {
138 $success = true;
139
140 foreach ($items as $item)
141 {
142 if (!isset($item['meta']))
143 {
144 $success = false;
145 break;
146 }
147
148 // get meta
149 $meta = $item['meta'];
150
151 // remote meta from item
152 unset($item['meta']);
153
154 if (!isset($item['box']))
155 {
156 $success = false;
157 break;
158 }
159
160 $box = $item['box'];
161
162 // remove ID
163 $box['ID'] = '';
164
165 $factory = new \FPFramework\Base\Factory();
166
167 $tz = wp_timezone();
168 $date_without_tz = $factory->getDate();
169 $date_with_tz = $factory->getDate()->setTimezone($tz);
170
171 $box['post_date'] = $date_with_tz->format('Y-m-d H:i:s');
172 $box['post_date_gmt'] = $date_without_tz->format('Y-m-d H:i:s');
173
174 // set publish status
175 if (in_array($publish_all, [0, 1]))
176 {
177 $box['post_status'] = ($publish_all == 0) ? 'draft' : 'publish';
178 }
179
180 // insert new box
181 if (!$new_box_id = firebox()->models->box->insert((object) $box))
182 {
183 $success = false;
184 break;
185 }
186
187 // add meta options for new box
188 update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta));
189 $success = $new_box_id;
190 }
191
192 return $success;
193 }
194
195 /**
196 * What the settings page will contain
197 *
198 * @return void
199 */
200 public function settingsPageContent()
201 {
202 $form = new Form(\FireBox\Core\Admin\Forms\Import::getSettings(), [
203 'fields_name_prefix' => 'firebox_import',
204 'section_name' => self::settings_name,
205 'class' => 'settings-ui-inner-fields'
206 ]);
207 $form->render();
208 }
209 }