| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Presets\Sources; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
class Preset_Source_Post extends Base_Source { |
| 14 |
|
| 15 |
public function get_id() { |
| 16 |
return 'post'; |
| 17 |
} |
| 18 |
|
| 19 |
public function on_sanitize(): bool { |
| 20 |
if ( ! is_user_logged_in() ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
if ( absint( $this->src()->post_author ) !== get_current_user_id() && ! current_user_can( 'edit_others_posts' ) ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @return array|\WP_Post|null |
| 33 |
* @throws Preset_Exception |
| 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 |
|
| 43 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 44 |
$post_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false; |
| 45 |
|
| 46 |
// ADDED: fallback for submit, where original query var is stored in referer URL. |
| 47 |
if ( ! $post_id && $var && ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
| 48 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 49 |
$referer = esc_url_raw( wp_unslash( $_REQUEST['_wp_http_referer'] ) ); |
| 50 |
$query = wp_parse_url( $referer, PHP_URL_QUERY ); |
| 51 |
if ( $query ) { |
| 52 |
parse_str( $query, $query_args ); |
| 53 |
if ( isset( $query_args[ $var ] ) ) { |
| 54 |
$post_id = absint( $query_args[ $var ] ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $post_id ) { |
| 61 |
throw new Preset_Exception( 'Empty Post ID' ); |
| 62 |
} |
| 63 |
|
| 64 |
return get_post( $post_id ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
protected function can_get_preset() { |
| 69 |
return ( parent::can_get_preset() && |
| 70 |
( |
| 71 |
absint( $this->src()->post_author ) === get_current_user_id() |
| 72 |
|| current_user_can( 'edit_others_posts' ) |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function source__post_meta() { |
| 78 |
|
| 79 |
if ( empty( $this->field_data['key'] ) ) { |
| 80 |
return ''; |
| 81 |
} |
| 82 |
|
| 83 |
$value = get_post_meta( $this->src()->ID, $this->field_data['key'], true ); |
| 84 |
|
| 85 |
if ( $this->is_repeater_val( $value ) ) { |
| 86 |
|
| 87 |
$prepared_value = array(); |
| 88 |
|
| 89 |
foreach ( $value as $index => $row ) { |
| 90 |
|
| 91 |
$prepared_row = array(); |
| 92 |
|
| 93 |
foreach ( $row as $item_key => $item_value ) { |
| 94 |
|
| 95 |
$item_key = $this->get_key_from_map( $item_key ); |
| 96 |
|
| 97 |
$prepared_row[ $item_key ] = $item_value; |
| 98 |
} |
| 99 |
|
| 100 |
$prepared_value[] = $prepared_row; |
| 101 |
|
| 102 |
} |
| 103 |
$value = $prepared_value; |
| 104 |
} elseif ( function_exists( 'jet_engine' ) |
| 105 |
&& jet_engine()->relations |
| 106 |
&& jet_engine()->relations->is_relation_key( $this->field_data['key'] ) ) { |
| 107 |
|
| 108 |
$info = jet_engine()->relations->get_relation_info( $this->field_data['key'] ); |
| 109 |
|
| 110 |
if ( ! $info ) { |
| 111 |
return ''; |
| 112 |
} |
| 113 |
|
| 114 |
$args = array( |
| 115 |
'post_id' => $this->src()->ID, |
| 116 |
'post_type_1' => $info['post_type_1'], |
| 117 |
'post_type_2' => $info['post_type_2'], |
| 118 |
); |
| 119 |
|
| 120 |
if ( $this->src()->post_type === $info['post_type_1'] ) { |
| 121 |
$args['from'] = $info['post_type_2']; |
| 122 |
} else { |
| 123 |
$args['from'] = $info['post_type_1']; |
| 124 |
} |
| 125 |
|
| 126 |
$value = jet_engine()->relations->get_related_posts( $args ); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
return $value; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @return array|string|string[] |
| 135 |
* @throws Preset_Exception |
| 136 |
*/ |
| 137 |
public function source__post_terms() { |
| 138 |
if ( empty( $this->field_data['key'] ) ) { |
| 139 |
throw new Preset_Exception( |
| 140 |
'Undefined `key` in `field_data`', |
| 141 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 142 |
$this->field_data |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$value = wp_get_post_terms( $this->src()->ID, $this->field_data['key'] ); |
| 147 |
|
| 148 |
if ( empty( $value ) || is_wp_error( $value ) ) { |
| 149 |
return ''; |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* Get expected format for Form Field |
| 154 |
*/ |
| 155 |
$format = $this->get_expected_format(); |
| 156 |
|
| 157 |
return $this->get_post_terms_by_format( $value, $format ); |
| 158 |
} |
| 159 |
|
| 160 |
protected function get_post_terms_by_format( $value, $format ) { |
| 161 |
switch ( $format ) { |
| 162 |
case 'list': |
| 163 |
case 'array': |
| 164 |
return array_map( |
| 165 |
function ( $term ) { |
| 166 |
return strval( $term->term_id ); |
| 167 |
}, |
| 168 |
$value |
| 169 |
); |
| 170 |
case 'custom': |
| 171 |
return $value; |
| 172 |
} |
| 173 |
|
| 174 |
return $value[0]->term_id ?? ''; |
| 175 |
} |
| 176 |
|
| 177 |
public function source__post_thumb() { |
| 178 |
return get_post_thumbnail_id( $this->src()->ID ); |
| 179 |
} |
| 180 |
|
| 181 |
public function is_repeater_val( $value ) { |
| 182 |
if ( is_array( $value ) && ! empty( $value ) ) { |
| 183 |
$value = array_values( $value ); |
| 184 |
|
| 185 |
return is_array( $value[0] ); |
| 186 |
} else { |
| 187 |
return false; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public function get_key_from_map( $repeater_key ) { |
| 192 |
|
| 193 |
foreach ( $this->fields_map as $field => $data ) { |
| 194 |
|
| 195 |
if ( 'post_meta' === $this->prop |
| 196 |
&& ! empty( $this->preset_data['key'] ) |
| 197 |
&& $repeater_key === $this->preset_data['key'] |
| 198 |
) { |
| 199 |
return $field; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return $repeater_key; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
} |
| 208 |
|