| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 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 |
$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_CAMPAIGN_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_CAMPAIGN_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 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 131 |
return file_get_contents($tmp_name); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Imports boxes data |
| 136 |
* |
| 137 |
* @param array $items |
| 138 |
* @param int $publish_all |
| 139 |
* |
| 140 |
* @return boolean |
| 141 |
*/ |
| 142 |
protected function importBoxes($items, $publish_all = 0) |
| 143 |
{ |
| 144 |
$success = true; |
| 145 |
|
| 146 |
foreach ($items as $item) |
| 147 |
{ |
| 148 |
if (!isset($item['meta'])) |
| 149 |
{ |
| 150 |
$success = false; |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
// get meta |
| 155 |
$meta = $item['meta']; |
| 156 |
|
| 157 |
// remote meta from item |
| 158 |
unset($item['meta']); |
| 159 |
|
| 160 |
if (!isset($item['box'])) |
| 161 |
{ |
| 162 |
$success = false; |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
$box = $item['box']; |
| 167 |
|
| 168 |
// remove ID |
| 169 |
$box['ID'] = ''; |
| 170 |
|
| 171 |
$factory = new \FPFramework\Base\Factory(); |
| 172 |
|
| 173 |
$tz = wp_timezone(); |
| 174 |
$date_without_tz = $factory->getDate(); |
| 175 |
$date_with_tz = $factory->getDate()->setTimezone($tz); |
| 176 |
|
| 177 |
$box['post_date'] = $date_with_tz->format('Y-m-d H:i:s'); |
| 178 |
$box['post_date_gmt'] = $date_without_tz->format('Y-m-d H:i:s'); |
| 179 |
|
| 180 |
// set publish status |
| 181 |
if (in_array($publish_all, [0, 1])) |
| 182 |
{ |
| 183 |
$box['post_status'] = ($publish_all == 0) ? 'draft' : 'publish'; |
| 184 |
} |
| 185 |
|
| 186 |
// insert new box |
| 187 |
if (!$new_box_id = firebox()->tables->box->insert((object) $box)) |
| 188 |
{ |
| 189 |
$success = false; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
// add meta options for new box |
| 194 |
update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta)); |
| 195 |
$success = $new_box_id; |
| 196 |
} |
| 197 |
|
| 198 |
return $success; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* What the settings page will contain |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function settingsPageContent() |
| 207 |
{ |
| 208 |
$form = new Form(\FireBox\Core\Admin\Forms\Import::getSettings(), [ |
| 209 |
'fields_name_prefix' => self::settings_name, |
| 210 |
'section_name' => self::settings_name, |
| 211 |
'class' => 'settings-ui-inner-fields', |
| 212 |
'button_label' => 'FPF_IMPORT' |
| 213 |
]); |
| 214 |
|
| 215 |
echo $form->render(); |
| 216 |
} |
| 217 |
} |