PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
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-form-record.php

preset-source-form-record.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at includes/presets/sources/preset-source-form-record.php

120 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Jet_Form_Builder\Presets\Sources;
4
5 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
6 use Jet_Form_Builder\Classes\Http\Http_Tools;
7 use Jet_Form_Builder\Classes\Tools;
8 use Jet_Form_Builder\Exceptions\Query_Builder_Exception;
9 use Jet_Form_Builder\Presets\Preset_Manager;
10 use JFB_Modules\Form_Record\Query_Views\Record_Fields_View;
11 use JFB_Modules\Form_Record\Query_Views\Record_View;
12 use JFB_Modules\Form_Record\Tools as Record_Tools;
13
14 // If this file is called directly, abort.
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 class Preset_Source_Form_Record extends Base_Source {
20
21 protected static $records_cache = array();
22 protected static $values_cache = array();
23
24 public function get_id() {
25 return 'form_record';
26 }
27
28 /**
29 * Form Record fields are mapped to form fields by name automatically.
30 *
31 * @return array
32 */
33 public function get_field_data() {
34 return array(
35 'prop' => $this->field,
36 );
37 }
38
39 public function get_result_on_prop() {
40 if ( 'password' === ( $this->field_args['field_type'] ?? '' ) ) {
41 return '';
42 }
43
44 return parent::get_result_on_prop();
45 }
46
47 /**
48 * @return false|object
49 */
50 public function query_source() {
51 $record_id = $this->get_record_id();
52
53 if ( ! $record_id ) {
54 return false;
55 }
56
57 if ( ! array_key_exists( $record_id, self::$records_cache ) ) {
58 try {
59 self::$records_cache[ $record_id ] = Record_View::findById( $record_id );
60 } catch ( Query_Builder_Exception $exception ) {
61 self::$records_cache[ $record_id ] = false;
62 }
63 }
64 $record = self::$records_cache[ $record_id ];
65
66 if ( ! is_array( $record ) || ! $this->can_view_record( $record ) ) {
67 return false;
68 }
69
70 if ( ! array_key_exists( $record_id, self::$values_cache ) ) {
71 $fields = array_filter(
72 Record_Fields_View::get_request( $record_id ),
73 array( $this, 'is_safe_record_field' )
74 );
75 self::$values_cache[ $record_id ] = iterator_to_array(
76 Record_Tools::iterate_request( $fields )
77 );
78 }
79
80 return (object) self::$values_cache[ $record_id ];
81 }
82
83 protected function is_safe_record_field( array $field ): bool {
84 $attrs = Tools::decode_json( $field['field_attrs'] ?? '' );
85
86 return 'password' !== ( $attrs['field_type'] ?? '' );
87 }
88
89 protected function can_view_record( array $record ): bool {
90 if (
91 empty( $record['id'] ) ||
92 absint( $record['form_id'] ?? 0 ) !== Preset_Manager::instance()->get_form_id()
93 ) {
94 return false;
95 }
96
97 $capability = Tab_Handler_Manager::get_form_records_access_capability();
98
99 if ( current_user_can( 'manage_options' ) || current_user_can( $capability ) ) {
100 return true;
101 }
102
103 return is_user_logged_in() && absint( $record['user_id'] ?? 0 ) === get_current_user_id();
104 }
105
106 protected function get_record_id(): int {
107 $query_var = sanitize_key( $this->preset_data['record_query_var'] ?? '' );
108
109 if ( ! $query_var ) {
110 return 0;
111 }
112
113 $query_args = Http_Tools::get_query();
114
115 return isset( $query_args[ $query_var ] ) && is_scalar( $query_args[ $query_var ] )
116 ? absint( $query_args[ $query_var ] )
117 : 0;
118 }
119 }
120