| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Presets\Sources; |
| 5 |
|
| 6 |
|
| 7 |
class Preset_Source_Post extends Base_Source { |
| 8 |
|
| 9 |
private $array_allowed; |
| 10 |
|
| 11 |
public function __construct( $field_data, $field_args, $preset_data ) { |
| 12 |
parent::__construct( $field_data, $field_args, $preset_data ); |
| 13 |
|
| 14 |
$this->set_array_allowed(); |
| 15 |
} |
| 16 |
|
| 17 |
private function set_array_allowed() { |
| 18 |
if ( isset( $this->field_args['type'] ) && isset( $this->field_args['array_allowed'] ) ) { |
| 19 |
$this->array_allowed = in_array( $this->field_args['type'], array( 'checkboxes' ) ) || ! empty( $this->field_args['array_allowed'] ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public function on_sanitize() { |
| 24 |
if ( ! is_user_logged_in() ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
if ( absint( $this->src()->post_author ) !== get_current_user_id() && ! current_user_can( 'edit_others_posts' ) ) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
public function query_source() { |
| 36 |
$post_from = ! empty( $this->preset_data['post_from'] ) ? $this->preset_data['post_from'] : 'current_post'; |
| 37 |
|
| 38 |
if ( 'current_post' === $post_from ) { |
| 39 |
$post_id = get_the_ID(); |
| 40 |
} else { |
| 41 |
$var = ! empty( $this->preset_data['query_var'] ) ? $this->preset_data['query_var'] : ''; |
| 42 |
$post_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? $_REQUEST[ $var ] : false; |
| 43 |
} |
| 44 |
|
| 45 |
if ( $post_id ) { |
| 46 |
return get_post( $post_id ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
protected function can_get_preset() { |
| 52 |
return ( parent::can_get_preset() && |
| 53 |
( |
| 54 |
absint( $this->src()->post_author ) === get_current_user_id() |
| 55 |
|| current_user_can( 'edit_others_posts' ) |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function _source__post_meta() { |
| 61 |
if ( empty( $this->field_data['key'] ) ) { |
| 62 |
return self::EMPTY; |
| 63 |
} |
| 64 |
|
| 65 |
$value = get_post_meta( $this->src()->ID, $this->field_data['key'], true ); |
| 66 |
|
| 67 |
if ( $this->is_repeater_val( $value ) ) { |
| 68 |
|
| 69 |
$prepared_value = array(); |
| 70 |
|
| 71 |
foreach ( $value as $index => $row ) { |
| 72 |
|
| 73 |
$prepared_row = array(); |
| 74 |
|
| 75 |
foreach ( $row as $item_key => $item_value ) { |
| 76 |
|
| 77 |
$item_key = $this->get_key_from_map( $item_key ); |
| 78 |
|
| 79 |
$prepared_row[ $item_key ] = $item_value; |
| 80 |
} |
| 81 |
|
| 82 |
$prepared_value[] = $prepared_row; |
| 83 |
|
| 84 |
} |
| 85 |
$value = $prepared_value; |
| 86 |
} else if ( function_exists( 'jet_engine' ) |
| 87 |
&& jet_engine()->relations |
| 88 |
&& jet_engine()->relations->is_relation_key( $this->field_data['key'] ) ) { |
| 89 |
|
| 90 |
$info = jet_engine()->relations->get_relation_info( $this->field_data['key'] ); |
| 91 |
|
| 92 |
if ( ! $info ) { |
| 93 |
return self::EMPTY; |
| 94 |
} |
| 95 |
|
| 96 |
$args = array( |
| 97 |
'post_id' => $this->src()->ID, |
| 98 |
'post_type_1' => $info['post_type_1'], |
| 99 |
'post_type_2' => $info['post_type_2'], |
| 100 |
); |
| 101 |
|
| 102 |
if ( $this->src()->post_type === $info['post_type_1'] ) { |
| 103 |
$args['from'] = $info['post_type_2']; |
| 104 |
} else { |
| 105 |
$args['from'] = $info['post_type_1']; |
| 106 |
} |
| 107 |
|
| 108 |
$value = jet_engine()->relations->get_related_posts( $args ); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
return $value; |
| 113 |
} |
| 114 |
|
| 115 |
public function _source__post_terms() { |
| 116 |
if ( empty( $field_data['key'] ) ) { |
| 117 |
return self::EMPTY; |
| 118 |
} |
| 119 |
|
| 120 |
$value = wp_get_post_terms( $this->src()->ID, $field_data['key'] ); |
| 121 |
|
| 122 |
if ( empty( $value ) || is_wp_error( $value ) ) { |
| 123 |
return ''; |
| 124 |
} else { |
| 125 |
if ( $this->array_allowed ) { |
| 126 |
$value = array_map( function ( $term ) { |
| 127 |
return strval( $term->term_id ); |
| 128 |
}, $value ); |
| 129 |
} else { |
| 130 |
$value = $value[0]; |
| 131 |
$value = $value->term_id; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $value; |
| 136 |
} |
| 137 |
|
| 138 |
public function _source__post_thumb() { |
| 139 |
return get_post_thumbnail_id( $this->src()->ID ); |
| 140 |
} |
| 141 |
|
| 142 |
public function is_repeater_val( $value ) { |
| 143 |
if ( is_array( $value ) && ! empty( $value ) ) { |
| 144 |
$value = array_values( $value ); |
| 145 |
|
| 146 |
return is_array( $value[0] ); |
| 147 |
} else { |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function get_key_from_map( $repeater_key ) { |
| 153 |
|
| 154 |
foreach ( $this->fields_map as $field => $data ) { |
| 155 |
|
| 156 |
if ( 'post_meta' === $this->prop |
| 157 |
&& ! empty( $this->preset_data['key'] ) |
| 158 |
&& $repeater_key == $this->preset_data['key'] ) { |
| 159 |
return $field; |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
return $repeater_key; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
} |