PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.1
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / includes / presets / sources / preset-source-post.php

preset-source-post.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.1, at includes/presets/sources/preset-source-post.php

193 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
43 $post_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? absint( $_REQUEST[ $var ] ) : false;
44 }
45
46 if ( ! $post_id ) {
47 throw new Preset_Exception( 'Empty Post ID' );
48 }
49
50 return get_post( $post_id );
51 }
52
53
54 protected function can_get_preset() {
55 return ( parent::can_get_preset() &&
56 (
57 absint( $this->src()->post_author ) === get_current_user_id()
58 || current_user_can( 'edit_others_posts' )
59 )
60 );
61 }
62
63 public function source__post_meta() {
64 if ( empty( $this->field_data['key'] ) ) {
65 return '';
66 }
67
68 $value = get_post_meta( $this->src()->ID, $this->field_data['key'], true );
69
70 if ( $this->is_repeater_val( $value ) ) {
71
72 $prepared_value = array();
73
74 foreach ( $value as $index => $row ) {
75
76 $prepared_row = array();
77
78 foreach ( $row as $item_key => $item_value ) {
79
80 $item_key = $this->get_key_from_map( $item_key );
81
82 $prepared_row[ $item_key ] = $item_value;
83 }
84
85 $prepared_value[] = $prepared_row;
86
87 }
88 $value = $prepared_value;
89 } elseif ( function_exists( 'jet_engine' )
90 && jet_engine()->relations
91 && jet_engine()->relations->is_relation_key( $this->field_data['key'] ) ) {
92
93 $info = jet_engine()->relations->get_relation_info( $this->field_data['key'] );
94
95 if ( ! $info ) {
96 return '';
97 }
98
99 $args = array(
100 'post_id' => $this->src()->ID,
101 'post_type_1' => $info['post_type_1'],
102 'post_type_2' => $info['post_type_2'],
103 );
104
105 if ( $this->src()->post_type === $info['post_type_1'] ) {
106 $args['from'] = $info['post_type_2'];
107 } else {
108 $args['from'] = $info['post_type_1'];
109 }
110
111 $value = jet_engine()->relations->get_related_posts( $args );
112
113 }
114
115 return $value;
116 }
117
118 /**
119 * @return array|string|string[]
120 * @throws Preset_Exception
121 */
122 public function source__post_terms() {
123 if ( empty( $this->field_data['key'] ) ) {
124 throw new Preset_Exception(
125 'Undefined `key` in `field_data`',
126 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
127 $this->field_data
128 );
129 }
130
131 $value = wp_get_post_terms( $this->src()->ID, $this->field_data['key'] );
132
133 if ( empty( $value ) || is_wp_error( $value ) ) {
134 return '';
135 }
136
137 /*
138 * Get expected format for Form Field
139 */
140 $format = $this->get_expected_format();
141
142 return $this->get_post_terms_by_format( $value, $format );
143 }
144
145 protected function get_post_terms_by_format( $value, $format ) {
146 switch ( $format ) {
147 case 'list':
148 case 'array':
149 return array_map(
150 function ( $term ) {
151 return strval( $term->term_id );
152 },
153 $value
154 );
155 case 'custom':
156 return $value;
157 }
158
159 return $value[0]->term_id ?? '';
160 }
161
162 public function source__post_thumb() {
163 return get_post_thumbnail_id( $this->src()->ID );
164 }
165
166 public function is_repeater_val( $value ) {
167 if ( is_array( $value ) && ! empty( $value ) ) {
168 $value = array_values( $value );
169
170 return is_array( $value[0] );
171 } else {
172 return false;
173 }
174 }
175
176 public function get_key_from_map( $repeater_key ) {
177
178 foreach ( $this->fields_map as $field => $data ) {
179
180 if ( 'post_meta' === $this->prop
181 && ! empty( $this->preset_data['key'] )
182 && $repeater_key === $this->preset_data['key']
183 ) {
184 return $field;
185 }
186 }
187
188 return $repeater_key;
189 }
190
191
192 }
193