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
post.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXE_Addon_Post_Field extends PMXE_Addon_Field { |
| 8 | |
| 9 | public function getPermalink( $value ) { |
| 10 | $entry = empty( $value ) ? false : get_post( $value ); |
| 11 | |
| 12 | if ( $entry ) { |
| 13 | return get_permalink( $entry->ID ); |
| 14 | } else { |
| 15 | return ''; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | public function getSlug( $value ) { |
| 20 | $entry = empty( $value ) ? false : get_post( $value ); |
| 21 | |
| 22 | if ( $entry ) { |
| 23 | return $entry->post_name; |
| 24 | } else { |
| 25 | return ''; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public function getTitle( $value ) { |
| 30 | $entry = empty( $value ) ? false : get_post( $value ); |
| 31 | |
| 32 | if ( $entry ) { |
| 33 | return $entry->post_title; |
| 34 | } else { |
| 35 | return ''; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function toString() { |
| 40 | |
| 41 | $format = $this->settings['post_value_format'] ?? 'id'; |
| 42 | $return_value = []; |
| 43 | |
| 44 | if ( ! is_array( $this->value ) ) { |
| 45 | $value = [ $this->value ]; |
| 46 | } else { |
| 47 | $value = $this->value; |
| 48 | } |
| 49 | |
| 50 | foreach ( $value as $current_value ) { |
| 51 | |
| 52 | switch ( $format ) { |
| 53 | case 'id': |
| 54 | $return_value[] = $current_value; |
| 55 | break; |
| 56 | case 'permalink': |
| 57 | $return_value[] = $this->getPermalink( $current_value ); |
| 58 | break; |
| 59 | case 'slug': |
| 60 | $return_value[] = $this->getSlug( $current_value ); |
| 61 | break; |
| 62 | default: |
| 63 | $return_value[] = $this->getTitle( $current_value ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return implode( $this->getImplode(), $return_value ); |
| 68 | } |
| 69 | |
| 70 | public static function getImportTemplate( $field, $name, $field_tpl_key, $implode_delimiter, $is_xml_template ) { |
| 71 | if ( $is_xml_template ) { |
| 72 | $field_template = '{' . $field_tpl_key . '}'; |
| 73 | } else { |
| 74 | $field_tpl_key = str_replace( "[1]", "", $field_tpl_key ); |
| 75 | |
| 76 | if ( $field['multiple'] ) { |
| 77 | if ( $implode_delimiter == "|" ) { |
| 78 | $field_template = '[str_replace("|", ",",{' . $field_tpl_key . '[1]})]'; |
| 79 | } else { |
| 80 | $field_template = '{' . $field_tpl_key . '[1]}'; |
| 81 | } |
| 82 | } else { |
| 83 | $field_template = '{' . $field_tpl_key . '[1]}'; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return [ |
| 88 | 'delim' => $implode_delimiter, |
| 89 | 'value' => $field_template |
| 90 | ]; |
| 91 | } |
| 92 | } |
| 93 |