checkbox.php
3 weeks ago
date.php
3 weeks ago
datetime.php
3 weeks ago
field.php
3 weeks ago
gallery.php
3 weeks ago
media.php
3 weeks ago
number.php
3 weeks ago
post.php
3 weeks ago
radio.php
3 weeks ago
repeater.php
3 weeks ago
select.php
3 weeks ago
switcher.php
3 weeks ago
text.php
3 weeks ago
time.php
3 weeks ago
toggle.php
3 weeks ago
user.php
3 weeks ago
repeater.php
229 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXE_Addon_Repeater_Field extends PMXE_Addon_Field { |
| 8 | |
| 9 | public function getValue() { |
| 10 | $val = parent::getValue() ?: []; |
| 11 | |
| 12 | return array_values( $val ); |
| 13 | } |
| 14 | |
| 15 | public function isOnePerLine() { |
| 16 | return $this->settings['repeater_field_item_per_line'] ?? false; |
| 17 | } |
| 18 | |
| 19 | public function shouldFillEmptyColumns() { |
| 20 | return $this->settings['repeater_field_fill_empty_columns'] ?? false; |
| 21 | } |
| 22 | |
| 23 | public function getSubField( $key, $index ) { |
| 24 | foreach ( $this->subfields as $subfield ) { |
| 25 | if ( $subfield['key'] === $key ) { |
| 26 | return PMXE_Addon_Field::from( |
| 27 | $subfield, |
| 28 | $this->resolver, |
| 29 | $this->settings, |
| 30 | $this->elName . '_' . $key, |
| 31 | $this->elNameNs, |
| 32 | $this->phpFunction, |
| 33 | $this, |
| 34 | $index, |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | public function getArticleByIndex( $row, $rowIndex ) { |
| 43 | $formatted_row = []; |
| 44 | foreach ( $row as $itemName => $itemValue ) { |
| 45 | $subfield = $this->getSubField( $itemName, $rowIndex ); |
| 46 | if ( ! $subfield ) { |
| 47 | continue; |
| 48 | } // There could be extraneous data in the row that is not a subfield. |
| 49 | $formatted_row[ $subfield->elName ] = $subfield->toString(); |
| 50 | } |
| 51 | |
| 52 | return $formatted_row; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Used by WP All Export to generate the import template to Bulk Edit/Migrate. |
| 57 | * |
| 58 | * @param array $field |
| 59 | * @param string $name |
| 60 | * @param string $field_tpl_key |
| 61 | * @param string $implode_delimiter |
| 62 | * @param boolean $is_xml_template |
| 63 | * |
| 64 | * @return array |
| 65 | */ |
| 66 | public static function getImportTemplate( $field, $name, $field_tpl_key, $implode_delimiter, $is_xml_template ) { |
| 67 | if ( $is_xml_template ) { |
| 68 | $template = [ |
| 69 | 'mode' => 'variable-xml', |
| 70 | 'foreach' => '{' . $field_tpl_key . '/row}', |
| 71 | 'rows' => [] |
| 72 | ]; |
| 73 | } else { |
| 74 | $template = [ |
| 75 | 'mode' => 'variable-csv', |
| 76 | 'separator' => $implode_delimiter, |
| 77 | 'rows' => [] |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | // We need to use this inverted delimiter for the sub fields so they don't use the same delimiter as the repeater section itself. |
| 82 | $inverted_delimiter = $implode_delimiter == ',' ? '|' : ','; |
| 83 | |
| 84 | if ( ! empty( $field['subfields'] ) ) { |
| 85 | foreach ( $field['subfields'] as $sub_field ) { |
| 86 | $sub_field_tpl_key = $name . '_' . strtolower( $sub_field['key'] ); |
| 87 | $sub_field_class = getFieldClass($sub_field); |
| 88 | $sub_field_template = $sub_field_class::getImportTemplate($sub_field, $sub_field['key'], $sub_field_tpl_key, $inverted_delimiter, $is_xml_template ); |
| 89 | $template['rows']['0'][$sub_field['key']] = $sub_field_template; |
| 90 | |
| 91 | if ( is_subclass_of( $sub_field_class, '\Wpae\AddonAPI\PMXE_Addon_Switcher_Field' ) ) { |
| 92 | |
| 93 | add_filter("pmxe_get_{$field['addon']}_addon_api_template_options_".$field['key'], function($templateOptions) use ($field,$sub_field){ |
| 94 | $templateOptions[ $sub_field['addon'] . '_switchers' ][ $field['key'] ]['rows']['0'][ $sub_field['key'] ] = 'no'; |
| 95 | return $templateOptions; |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $template; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Empty on purpose, repeaters are handled differently in `modifyArticle` and `appendNewArticles` below. |
| 107 | */ |
| 108 | public function toString() { |
| 109 | return ''; |
| 110 | } |
| 111 | |
| 112 | public function exportXml( $article, $rows, $xmlWriter ) { |
| 113 | $elName = normalizeElementName( $this->elName ); |
| 114 | $xmlWriter->beginElement( $this->elNameNs, $elName, null ); |
| 115 | |
| 116 | foreach ( $rows as $rowIndex => $row ) { |
| 117 | $xmlWriter->startElement( "row" ); |
| 118 | |
| 119 | foreach ( $row as $itemName => $itemValue ) { |
| 120 | $subfield = $this->getSubField( $itemName, $rowIndex ); |
| 121 | if ( ! $subfield ) { |
| 122 | continue; |
| 123 | } |
| 124 | $subfield_value = $subfield->toString(); |
| 125 | $subfield->exportXml( $article, $subfield_value, $xmlWriter ); |
| 126 | } |
| 127 | |
| 128 | $xmlWriter->closeElement(); |
| 129 | } |
| 130 | |
| 131 | $xmlWriter->closeElement(); |
| 132 | |
| 133 | return $article; |
| 134 | } |
| 135 | |
| 136 | public function exportCsv($article, $value, $preview = false) |
| 137 | { |
| 138 | $rows = $this->value; |
| 139 | $delimiter = $this->getInvertedDelimiter(); |
| 140 | |
| 141 | // Create one row per subfield value |
| 142 | if ( $this->isOnePerLine() ) { |
| 143 | $first_row = array_shift( $rows ); |
| 144 | $formatted_row = $this->getArticleByIndex( $first_row, 0 ); |
| 145 | |
| 146 | return array_merge( $article, $formatted_row ); |
| 147 | } |
| 148 | |
| 149 | // Merge all values into one row per subfield |
| 150 | $formatted_row = []; |
| 151 | |
| 152 | foreach ( $rows as $rowIndex => $row ) { |
| 153 | foreach ( $row as $itemName => $itemValue ) { |
| 154 | $subfield = $this->getSubField( $itemName, $rowIndex ); |
| 155 | if ( ! $subfield ) { |
| 156 | continue; |
| 157 | } |
| 158 | $formatted_row[ $subfield->elName ][] = $preview ? $subfield->toHtml() : $subfield->toString(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | foreach ( $formatted_row as $element_name => $value ) { |
| 163 | wp_all_export_write_article( $article, $element_name, implode( $delimiter, $value ) ); |
| 164 | } |
| 165 | |
| 166 | return $article; |
| 167 | } |
| 168 | |
| 169 | public function modifyArticle( $article, $xmlWriter, $preview = false ) { |
| 170 | $rows = $this->value; |
| 171 | $is_xml_export = PMXE_Addon_Exporter::isXmlExport( $xmlWriter ); |
| 172 | $is_custom_xml_export = PMXE_Addon_Exporter::isCustomXmlExport(); |
| 173 | |
| 174 | // Custom XML exports are handled differently. |
| 175 | if ( $is_custom_xml_export ) { |
| 176 | return $this->exportCustomXml( $article, serialize( $rows ) ); |
| 177 | } |
| 178 | |
| 179 | if ( $is_xml_export ) { |
| 180 | $this->exportXml( $article, $rows, $xmlWriter ); |
| 181 | |
| 182 | return $article; |
| 183 | } |
| 184 | |
| 185 | return $this->exportCsv( $article, $this->value, $preview ); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Duplicate the base article for each row in the repeater. |
| 190 | */ |
| 191 | public function appendNewArticles( $baseArticle ) { |
| 192 | if ( ! $this->isOnePerLine() ) { |
| 193 | return []; |
| 194 | } |
| 195 | |
| 196 | $rows = $this->value; |
| 197 | $articles = []; |
| 198 | array_shift( $rows ); |
| 199 | |
| 200 | foreach ( $rows as $rowIndex => $row ) { |
| 201 | $formatted_row = $this->getArticleByIndex( $row, $rowIndex + 1 ); |
| 202 | |
| 203 | $articles[] = [ |
| 204 | 'content' => $this->shouldFillEmptyColumns() ? |
| 205 | array_merge( $baseArticle, $formatted_row ) : |
| 206 | $formatted_row, |
| 207 | 'settings' => $this->settings |
| 208 | ]; |
| 209 | } |
| 210 | |
| 211 | return $articles; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Add subfields as headers to the export file. |
| 216 | */ |
| 217 | public function getHeaders() { |
| 218 | $headers = [ |
| 219 | "-{$this->elName}", |
| 220 | ]; |
| 221 | |
| 222 | foreach ( $this->subfields as $subfield ) { |
| 223 | $headers[] = $this->elName . '_' . $subfield['key']; |
| 224 | } |
| 225 | |
| 226 | return $headers; |
| 227 | } |
| 228 | } |
| 229 |