admin.php
3 weeks ago
base.php
3 weeks ago
data-importer.php
3 weeks ago
helpers.php
3 weeks ago
importer.php
3 weeks ago
manager.php
3 weeks ago
parser.php
3 weeks ago
post-data-importer.php
3 weeks ago
rest.php
3 weeks ago
updater.php
3 weeks ago
view.php
3 weeks ago
view.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXI_Addon_View { |
| 8 | |
| 9 | public string $path = WP_ALL_IMPORT_ROOT_DIR . '/addon-api'; |
| 10 | |
| 11 | public PMXI_Addon_Base $addon; |
| 12 | public array $groups; |
| 13 | public array $group; |
| 14 | public array $fields = []; |
| 15 | public array $values = []; |
| 16 | public array $options = []; |
| 17 | |
| 18 | public string $addonId; |
| 19 | public string $type; |
| 20 | public ?string $subtype; |
| 21 | public ?string $groupId; |
| 22 | |
| 23 | public function __construct( |
| 24 | $addonId, |
| 25 | $type, |
| 26 | $subtype = null, |
| 27 | $groupId = null |
| 28 | ) { |
| 29 | $this->addonId = $addonId; |
| 30 | $this->type = $type; |
| 31 | $this->subtype = $subtype; |
| 32 | $this->groupId = $groupId; |
| 33 | |
| 34 | // Cache all the data we need to render the view |
| 35 | $this->addon = PMXI_Addon_Manager::get_addon( $addonId ); |
| 36 | $this->groups = $this->addon->groups( $type, $subtype ); |
| 37 | $this->options = $this->getOptions(); |
| 38 | $this->values = $this->getValues(); |
| 39 | |
| 40 | // If a group is selected, cache the group and its fields |
| 41 | if ( $groupId ) { |
| 42 | $this->group = $this->addon->getGroupById( $groupId, $type, $subtype ); |
| 43 | $this->fields = $this->addon->getFieldsByGroup( $groupId, $type, $subtype ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function getOptions() { |
| 48 | $import = new \PMXI_Import_Record(); |
| 49 | $input = new \PMXI_Input(); |
| 50 | $import_id = $input->get( 'id' ) ?? $input->get( 'import_id' ); |
| 51 | |
| 52 | // Get import ID from CLI arguments. |
| 53 | if ( empty( $import_id ) && \PMXI_Plugin::getInstance()->isCli() ) { |
| 54 | $import_id = wp_all_import_get_import_id(); |
| 55 | } |
| 56 | |
| 57 | if ( ! empty( $import_id ) ) { |
| 58 | $import->getById( $import_id ); |
| 59 | } |
| 60 | |
| 61 | $session = new \PMXI_Handler(); |
| 62 | $templateId = ! empty( $session->is_loaded_template ) ? $session->is_loaded_template : false; |
| 63 | |
| 64 | // Initialize options to empty array to ensure we always return an array |
| 65 | $options = []; |
| 66 | |
| 67 | if ( $templateId ) { |
| 68 | $defaults = $this->addon->defaultOptions( $this->type, $this->subtype ); |
| 69 | $template = new \PMXI_Template_Record(); |
| 70 | |
| 71 | if ( ! $template->getById( $templateId )->isEmpty() ) { |
| 72 | $templateOptions = ! empty( $template->options ) ? $template->options : []; |
| 73 | $options = $templateOptions + $defaults; |
| 74 | } |
| 75 | } elseif ( ! $import->isEmpty() ) { |
| 76 | $options = $import->options ?? []; |
| 77 | } else { |
| 78 | $options = $session->options ?? []; |
| 79 | } |
| 80 | |
| 81 | return $options; |
| 82 | } |
| 83 | |
| 84 | public function getAddonValue( $key, $suffix = null, $default = null ) { |
| 85 | $suffix = $suffix ? '_' . $suffix : ''; |
| 86 | |
| 87 | return $this->options[ $this->addon->slug . $suffix ][ $key ] ?? $default; |
| 88 | } |
| 89 | |
| 90 | public function getValues() { |
| 91 | $options = $this->getOptions(); |
| 92 | |
| 93 | return $options[ $this->addon->slug ] ?? []; |
| 94 | } |
| 95 | |
| 96 | public function existingMetaKeys( $type, $subtype ) { |
| 97 | return array_map( fn( $field ) => $field['key'], $this->addon->fields( $type, $subtype ) ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param array $importOptions |
| 102 | * |
| 103 | * @return void |
| 104 | * @throws \Exception |
| 105 | */ |
| 106 | public function renderTabs( array $importOptions ) { |
| 107 | $subtype = $importOptions['taxonomy_type']; |
| 108 | |
| 109 | if ( ! $this->addon->isAvailableForType( $this->type, $importOptions ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | view( 'accordion', [ |
| 114 | 'addon' => $this->addon, |
| 115 | 'groups' => $this->groups, |
| 116 | 'type' => $this->type, |
| 117 | 'subtype' => $subtype, |
| 118 | 'importOptions' => $importOptions, |
| 119 | ] ); |
| 120 | } |
| 121 | |
| 122 | public function renderUpdate( array $importOptions ) { |
| 123 | $subtype = $importOptions['taxonomy_type']; |
| 124 | $updateOptions = $importOptions['update_addons'][ $this->addon->slug ] ?? $this->addon->defaultUpdateOptions(); |
| 125 | $existingMetaKeys = $this->existingMetaKeys( $this->type, $subtype ); |
| 126 | |
| 127 | view( 'screens/update', [ |
| 128 | 'addon' => $this->addon, |
| 129 | 'groups' => $this->groups, |
| 130 | 'type' => $this->type, |
| 131 | 'subtype' => $subtype, |
| 132 | 'options' => $updateOptions, |
| 133 | 'prefix' => 'update_addons[' . $this->addon->slug . ']', |
| 134 | 'prefix_id' => 'update_addons_' . $this->addon->slug, |
| 135 | 'existing_meta_keys' => $existingMetaKeys, |
| 136 | ] ); |
| 137 | } |
| 138 | |
| 139 | public function renderConfirmDataToImport( array $importOptions ) { |
| 140 | $subtype = $importOptions['taxonomy_type']; |
| 141 | $updateOptions = $importOptions['update_addons'][ $this->addon->slug ] ?? $this->addon->defaultUpdateOptions(); |
| 142 | |
| 143 | view( 'screens/confirm-data-to-import', [ |
| 144 | 'addon' => $this->addon, |
| 145 | 'groups' => $this->groups, |
| 146 | 'type' => $this->type, |
| 147 | 'subtype' => $subtype, |
| 148 | 'post' => $updateOptions |
| 149 | ] ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Return the fields inside the accordion container |
| 154 | * @return string |
| 155 | * @throws \Exception |
| 156 | */ |
| 157 | public function getFieldsHtml() { |
| 158 | ob_start(); |
| 159 | |
| 160 | view( 'container-start', [ 'group' => $this->group ] ); |
| 161 | |
| 162 | foreach ( $this->fields as $field ) { |
| 163 | PMXI_Addon_Field::from( $field, $this )->show(); |
| 164 | } |
| 165 | |
| 166 | if ( ! count( $this->fields ) ) { |
| 167 | view( 'no-fields', [] ); |
| 168 | } |
| 169 | |
| 170 | view( 'container-end', [ 'group' => $this->group ] ); |
| 171 | |
| 172 | return ob_get_clean(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Create a new instance of the view |
| 177 | */ |
| 178 | public static function create( |
| 179 | string $addonId, |
| 180 | string $type, |
| 181 | ?string $subtype = null, |
| 182 | ?string $groupId = null |
| 183 | ) { |
| 184 | return new self( $addonId, $type, $subtype, $groupId ); |
| 185 | } |
| 186 | } |
| 187 |