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
media.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXE_Addon_Media_Field extends PMXE_Addon_Field { |
| 8 | |
| 9 | public function getMediaId( $value ) { |
| 10 | if ( is_numeric( $value ) ) { |
| 11 | return $value; |
| 12 | } elseif ( is_array( $value ) ) { |
| 13 | if ( isset( $value['id'] ) ) { |
| 14 | return $value['id']; |
| 15 | } |
| 16 | if ( isset( $value['ID'] ) ) { |
| 17 | return $value['ID']; |
| 18 | } |
| 19 | |
| 20 | return ''; |
| 21 | } elseif ( is_string( $value ) ) { |
| 22 | $id = attachment_url_to_postid( $value ); |
| 23 | |
| 24 | return $id > 0 ? $id : ''; |
| 25 | } |
| 26 | |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | public function getMediaUrl( $value ) { |
| 31 | return wp_get_attachment_url( $this->getMediaId( $value ) ); |
| 32 | } |
| 33 | |
| 34 | public function getFileName( $value ) { |
| 35 | $url = $this->getMediaUrl( $value ); |
| 36 | if ( empty( $url ) ) { |
| 37 | return ''; |
| 38 | } |
| 39 | $path = wp_parse_url( $url, PHP_URL_PATH ); |
| 40 | |
| 41 | return basename( $path ); |
| 42 | } |
| 43 | |
| 44 | public function toString() { |
| 45 | $format = $this->settings['value_format'] ?? 'url'; |
| 46 | |
| 47 | switch ( $format ) { |
| 48 | case 'id': |
| 49 | return $this->getMediaId( $this->value ); |
| 50 | case 'filename': |
| 51 | return $this->getFileName( $this->value ); |
| 52 | default: |
| 53 | return $this->getMediaUrl( $this->value ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function exportCustomXml( $article, $value, $write = true ) { |
| 58 | |
| 59 | $this->local_value = $value; |
| 60 | |
| 61 | $formatted_values = $this->toString(); |
| 62 | |
| 63 | $exported_value = $this->runPhpFunction( $formatted_values ); |
| 64 | |
| 65 | // By default we write the values to $article and return it. |
| 66 | // But if !$write we return the list of subfields we built instead. |
| 67 | if ( $write ) { |
| 68 | wp_all_export_write_article( $article, $this->elName, $exported_value ); |
| 69 | |
| 70 | return $article; |
| 71 | } else { |
| 72 | return $exported_value; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public static function getImportTemplate( $field, $name, $field_tpl_key, $implode_delimiter, $is_xml_template ) { |
| 77 | return [ |
| 78 | 'search_in_media' => 1, |
| 79 | 'url' => '{' . $field_tpl_key . '}' |
| 80 | ]; |
| 81 | } |
| 82 | } |
| 83 |