PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / trunk
LatePoint – Calendar Booking Plugin for Appointments and Events vtrunk
5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / meta_model.php
latepoint / lib / models Last commit date
activity_model.php 3 months ago agent_meta_model.php 3 months ago agent_model.php 3 months ago booking_meta_model.php 3 months ago booking_model.php 19 hours ago bundle_meta_model.php 3 months ago bundle_model.php 1 week ago cart_item_model.php 3 months ago cart_meta_model.php 3 months ago cart_model.php 2 weeks ago connector_model.php 3 months ago customer_meta_model.php 3 months ago customer_model.php 1 month ago invoice_model.php 2 weeks ago join_bundles_services_model.php 3 months ago location_category_model.php 3 months ago location_model.php 3 months ago meta_model.php 3 months ago model.php 2 days ago off_period_model.php 3 months ago order_intent_meta_model.php 3 months ago order_intent_model.php 1 week ago order_item_model.php 3 months ago order_meta_model.php 3 months ago order_model.php 1 month ago otp_model.php 3 months ago payment_request_model.php 3 months ago process_job_model.php 3 months ago process_model.php 1 month ago recurrence_model.php 3 months ago service_category_model.php 3 months ago service_meta_model.php 3 months ago service_model.php 3 months ago session_model.php 3 months ago settings_model.php 3 months ago step_settings_model.php 3 months ago transaction_intent_model.php 3 months ago transaction_model.php 3 months ago transaction_refund_model.php 3 months ago work_period_model.php 3 months ago
meta_model.php
161 lines
1 <?php
2
3 class OsMetaModel extends OsModel {
4 var $id,
5 $meta_key,
6 $meta_value,
7 $object_id,
8 $created_at,
9 $updated_at;
10
11 protected static $encrypted_settings = array();
12
13 function __construct( $object_id = false ) {
14 $this->nice_names = array();
15 $this->object_id = $object_id;
16 parent::__construct();
17 }
18
19 public function delete_by_key( $meta_key, $object_id ) {
20 if ( ! $object_id ) {
21 return false;
22 }
23 $meta_to_delete = $this->where(
24 array(
25 'meta_key' => $meta_key,
26 'object_id' => $object_id,
27 )
28 )->get_results_as_models();
29 if ( $meta_to_delete ) {
30 foreach ( $meta_to_delete as $meta_obj ) {
31 $meta_obj->delete();
32 }
33 }
34 return true;
35 }
36
37 public function save_by_key( $meta_key, $meta_value, $object_id = false ) {
38 if ( ! $object_id ) {
39 $object_id = $this->object_id;
40 }
41 if ( ! $object_id ) {
42 return false;
43 }
44 $existing_meta = $this->where(
45 array(
46 'meta_key' => $meta_key,
47 'object_id' => $object_id,
48 )
49 )->set_limit( 1 )->get_results_as_models();
50 if ( $existing_meta ) {
51 $existing_meta->meta_value = self::prepare_value( $meta_key, $meta_value );
52 if ( empty( $existing_meta->meta_value ) ) {
53 return $existing_meta->delete();
54 }
55 return $existing_meta->save();
56 } else {
57 $new_meta = $this;
58 $new_meta->object_id = $object_id;
59 $new_meta->meta_key = $meta_key;
60 $new_meta->meta_value = self::prepare_value( $meta_key, $meta_value );
61 return $new_meta->save();
62 }
63 }
64
65 private static function prepare_value( $meta_key, $meta_value ) {
66 if ( in_array( $meta_key, self::$encrypted_settings ) ) {
67 $meta_value = OsEncryptHelper::encrypt_value( $meta_value );
68 }
69 return $meta_value;
70 }
71
72 public function get_by_key( $meta_key, $object_id = false, $default = false ) {
73 if ( ! $object_id ) {
74 $object_id = $this->object_id;
75 }
76 if ( ! $object_id ) {
77 return $default;
78 }
79 $record = $this->where(
80 array(
81 'meta_key' => $meta_key,
82 'object_id' => $object_id,
83 )
84 )->set_limit( 1 )->get_results_as_models();
85 if ( $record ) {
86 if ( in_array( $meta_key, self::$encrypted_settings ) ) {
87 return OsEncryptHelper::decrypt_value( $record->meta_value );
88 } else {
89 return $record->meta_value;
90 }
91 } else {
92 return $default;
93 }
94 }
95
96 public function get_by_object_id( $object_id = false, $default = [] ) {
97 if ( ! $object_id ) {
98 $object_id = $this->object_id;
99 }
100 if ( ! $object_id ) {
101 return $default;
102 }
103 $records = $this->where( array( 'object_id' => $object_id ) )->get_results();
104 if ( $records ) {
105 $metas = [];
106 foreach ( $records as $record ) {
107 $value = in_array( $record->meta_key, self::$encrypted_settings ) ? OsEncryptHelper::decrypt_value( $record->meta_value ) : $record->meta_value;
108 $metas[ $record->meta_key ] = $value;
109 }
110 return $metas;
111 } else {
112 return $default;
113 }
114 }
115
116 public function get_object_id_by_value( $meta_key, $meta_value ) {
117 if ( ! $meta_value || ! $meta_key ) {
118 return false;
119 }
120 $record = $this->select( 'object_id' )->where(
121 array(
122 'meta_key' => $meta_key,
123 'meta_value' => $meta_value,
124 )
125 )->set_limit( 1 )->get_results_as_models();
126 if ( $record ) {
127 return $record->object_id;
128 } else {
129 return false;
130 }
131 }
132
133
134 protected function allowed_params( $role = 'admin' ) {
135 $allowed_params = array(
136 'object_id',
137 'meta_key',
138 'meta_value',
139 );
140 return $allowed_params;
141 }
142
143 protected function params_to_save( $role = 'admin' ) {
144 $params_to_save = array(
145 'object_id',
146 'meta_key',
147 'meta_value',
148 );
149 return $params_to_save;
150 }
151
152 protected function properties_to_validate() {
153 $validations = array(
154 'object_id' => array( 'presence' ),
155 'meta_key' => array( 'presence' ),
156 'meta_value' => array( 'presence' ),
157 );
158 return $validations;
159 }
160 }
161