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

207 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.1.23 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2024 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 // run a quick security check
54 if (!check_admin_referer('fpf_form_nonce_firebox_import', 'fpf_form_nonce_firebox_import'))
55 {
56 return; // get out if we didn't click the Activate button
57 }
58
59 if (!isset($_FILES['file']))
60 {
61 return;
62 }
63
64 $file = $_FILES['file'];
65
66 // ensure a file was given
67 if (!is_array($file) || !isset($file['name']) || empty($file['name']))
68 {
69 \FPFramework\Libs\AdminNotice::displayError(fpframework()->_('FPF_PLEASE_SELECT_A_FILE_TO_UPLOAD'));
70 return;
71 }
72
73 $ext = explode('.', $file['name']);
74
75 // ensure given file plugin was given
76 if (!in_array($ext[count($ext) - 1], ['fbox']))
77 {
78 \FPFramework\Libs\AdminNotice::displayError(fpframework()->_('FPF_PLEASE_CHOOSE_A_VALID_FILE'));
79 return;
80 }
81
82 $publish_all = isset($input['publish_all']) ? $input['publish_all'] : 0;
83
84 // read file contents
85 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
86 $data = file_get_contents($file['tmp_name']);
87
88 // if empty data file then abort
89 if (empty($data))
90 {
91 \FPFramework\Libs\AdminNotice::displayError(fpframework()->_('FPF_FILE_EMPTY'));
92 return;
93 }
94
95 if (!$items = json_decode($data, true))
96 {
97 \FPFramework\Libs\AdminNotice::displayError(firebox()->_('FB_CAMPAIGN_IMPORT_CONTENTS_ERROR'));
98 return;
99 }
100
101 if (is_null($items))
102 {
103 $items = [];
104 }
105
106 if (!$items)
107 {
108 return;
109 }
110
111 // import all boxes
112 if (!$new_box_id = $this->importBoxes($items, $publish_all))
113 {
114 \FPFramework\Libs\AdminNotice::displayError(firebox()->_('FB_CAMPAIGN_IMPORT_CONTENTS_ERROR'));
115 return;
116 }
117
118 \FPFramework\Libs\AdminNotice::displaySuccess(fpframework()->_('FPF_ITEMS_SAVED'));
119 return $new_box_id;
120 }
121
122 /**
123 * Imports boxes data
124 *
125 * @param array $items
126 * @param int $publish_all
127 *
128 * @return boolean
129 */
130 protected function importBoxes($items, $publish_all = 0)
131 {
132 $success = true;
133
134 foreach ($items as $item)
135 {
136 if (!isset($item['meta']))
137 {
138 $success = false;
139 break;
140 }
141
142 // get meta
143 $meta = $item['meta'];
144
145 // remote meta from item
146 unset($item['meta']);
147
148 if (!isset($item['box']))
149 {
150 $success = false;
151 break;
152 }
153
154 $box = $item['box'];
155
156 // remove ID
157 $box['ID'] = '';
158
159 $factory = new \FPFramework\Base\Factory();
160
161 $tz = wp_timezone();
162 $date_without_tz = $factory->getDate();
163 $date_with_tz = $factory->getDate()->setTimezone($tz);
164
165 $box['post_date'] = $date_with_tz->format('Y-m-d H:i:s');
166 $box['post_date_gmt'] = $date_without_tz->format('Y-m-d H:i:s');
167
168 \FireBox\Core\Helpers\Form\Form::ensureUniqueFormIDs($box['post_content']);
169
170 // set publish status
171 if (in_array($publish_all, [0, 1]))
172 {
173 $box['post_status'] = ($publish_all == 0) ? 'draft' : 'publish';
174 }
175
176 // insert new box
177 if (!$new_box_id = firebox()->tables->box->insert((object) $box))
178 {
179 $success = false;
180 break;
181 }
182
183 // add meta options for new box
184 update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta));
185 $success = $new_box_id;
186 }
187
188 return $success;
189 }
190
191 /**
192 * What the settings page will contain
193 *
194 * @return void
195 */
196 public function settingsPageContent()
197 {
198 $form = new Form(\FireBox\Core\Admin\Forms\Import::getSettings(), [
199 'fields_name_prefix' => self::settings_name,
200 'section_name' => self::settings_name,
201 'class' => 'settings-ui-inner-fields',
202 'button_label' => 'FPF_IMPORT'
203 ]);
204
205 echo $form->render(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
206 }
207 }