PluginProbe
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads / trunk
Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads vtrunk
1.2.1 1.2.0 1.1.7 trunk 1.0.0 1.1.0 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6
siteleads / lib / Core / DataHelper.php

DataHelper.php in Lead Generation Contact Widget & AI Chatbot: Chat Button, Phone Call, Telegram, Email – SiteLeads trunk, at lib/Core/DataHelper.php

234 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SiteLeads\Core;
4
5 use SiteLeads\Constants;
6 use SiteLeads\Core\LodashBasic;
7 use SiteLeads\Utils;
8
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13 class DataHelper {
14
15 protected $settingPath = null;
16
17 /**
18 * @var array|null All the settings from the admin page of the plugin plugin
19 */
20 protected static $pluginData = null;
21 protected static $pluginDataPost = null;
22 protected $data = null;
23
24 public function __construct( $settingPath = null ) {
25 if ( $settingPath ) {
26 $this->settingPath = $settingPath;
27 }
28 }
29
30 public function setStyle() {
31 /**
32 * Implement if needed
33 */
34 }
35
36 public function setProp( $path, $value, $options = array() ) {
37 $mergedOptions = $this->getMergedOptions( $options );
38 $absolutePath = $this->getAbsolutePropPath( $path, $mergedOptions );
39 $this->setData( $absolutePath, $value, $mergedOptions );
40 }
41
42 public function getStyle( $path, $defaultValue = null, $options = array() ) {
43 $mergedOptions = $this->getMergedOptions( $options );
44 $absolutePath = $this->getAbsoluteStylePath( $path, $mergedOptions );
45
46 return $this->getData( $absolutePath, $defaultValue, $mergedOptions );
47 }
48
49 public function getProp( $path, $defaultValue = null, $options = array() ) {
50 $mergedOptions = $this->getMergedOptions( $options );
51 $absolutePath = $this->getAbsolutePropPath( $path, $mergedOptions );
52
53 return $this->getData( $absolutePath, $defaultValue, $mergedOptions );
54 }
55
56 public function getSubProp( $path, $defaultValue = null, $options = array() ) {
57 $mergedOptions = $this->getMergedOptions( $options );
58 $absolutePath = $this->getAbsoluteSubPropPath( $path, $mergedOptions );
59
60 return $this->getData( $absolutePath, $defaultValue, $mergedOptions );
61 }
62
63 public function getMergedOptions( $options ) {
64 $defaultOptions = array(
65 'styledComponent' => null,
66 'media' => 'desktop',
67 'state' => 'normal',
68 'fromRoot' => false,
69 'unset' => false,
70 );
71
72 return LodashBasic::merge( $defaultOptions, $options );
73 }
74
75 public function getAbsoluteStylePath( $path, $options = array() ) {
76 return $this->getAbsolutePath( $path, array_merge( $options, array( 'prefix' => 'style' ) ) );
77 }
78
79 public function getAbsolutePropPath( $path, $options = array() ) {
80 return $this->getAbsolutePath( $path, array_merge( $options, array( 'prefix' => 'props' ) ) );
81 }
82
83 public function getAbsoluteSubPropPath( $path, $options = array() ) {
84 return $this->getAbsolutePath( $path, array_merge( $options, array( 'prefix' => '' ) ) );
85 }
86
87 public function getAbsolutePath( $relativePath, $options = array() ) {
88 list ( 'prefix' => $prefix, 'media' => $media, 'state' => $state, 'styledComponent' => $styledComponent ) = $options;
89
90 $paths = $prefix ? array( $prefix ) : array();
91 if ( $styledComponent ) {
92 $paths[] = "descendants.$styledComponent";
93 }
94 if ( $media !== 'desktop' ) {
95 $paths[] = "media.$media";
96 }
97 if ( $state !== 'normal' ) {
98 $paths[] = "state.$state";
99 }
100
101 if ( $relativePath ) {
102 $paths[] = $relativePath;
103 }
104 $joinedPaths = implode( '.', $paths );
105
106 return $joinedPaths;
107 }
108
109 public function getData( $path, $defaultValue ) {
110 $source = $this->getSourceData();
111
112 return LodashBasic::get( $source, $path, $defaultValue );
113 }
114
115 public function setData( $path, $newValue ) {
116 $this->getSourceData();
117
118 LodashBasic::set( $this->data, $path, $newValue );
119 }
120
121 public function getSourceData() {
122
123 if ( ! $this->settingPath ) {
124 // translators: %s is the class name
125 throw new \Exception( sprintf( esc_html__( 'settingPath property is not set in %s', 'siteleads' ), esc_html( get_class( $this ) ) ) );
126 }
127
128 if ( ! $this->data ) {
129 $pluginData = static::getPluginData();
130 $this->data = LodashBasic::get( $pluginData, $this->settingPath, array() );
131 }
132
133 return $this->data;
134 }
135
136 public static function getPluginData() {
137 if ( ! static::$pluginData ) {
138 static::$pluginData = static::getDataFromDatabase();
139 }
140
141 return static::$pluginData;
142 }
143
144 public static function getPluginDataPost() {
145 if ( empty( static::$pluginDataPost ) ) {
146 static::getPluginData();
147 }
148
149 return static::$pluginDataPost;
150 }
151
152 public static function getPreviewedWidgetId() {
153 return Utils::getPreviewedWidgetId();
154 }
155
156 public static function getLivePreviewedWidgetId() {
157 return Utils::getLivePreviewedWidgetId();
158 }
159
160 public static function getDataFromDatabase() {
161 $query = new \WP_Query(
162 array(
163 'post_type' => Constants::$siteLeadsDataPostType,
164 'post_status' => array( 'draft', 'publish' ),
165 'no_found_rows' => true,
166 'post_per_page' => 1,
167 )
168 );
169 $pluginData = array();
170 if ( $query->have_posts() ) {
171 $post = $query->next_post();
172
173 $widgetId = static::getPreviewedWidgetId();
174 if ( ! $widgetId ) {
175 $widgetId = static::getLivePreviewedWidgetId();
176 }
177
178 if ( $widgetId ) {
179 $data = get_transient( 'siteleads_preview_data' );
180
181 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a preview request, we just check a boolean flag in the request to decide whether to use the transient data or not
182 $skip_widget_preview_transient_data = LodashBasic::has( $_REQUEST, 'skip-widget-preview-transient-data' ) && rest_sanitize_boolean( LodashBasic::get( $_REQUEST, 'skip-widget-preview-transient-data' ) );
183 if ( $data && ! $skip_widget_preview_transient_data ) {
184 $post->post_content = $data;
185 }
186 }
187
188 static::$pluginDataPost = $post;
189
190 $pluginData = json_decode( static::$pluginDataPost->post_content, true );
191 }
192
193 return $pluginData;
194 }
195
196 public function saveChanges() {
197 $pluginData = static::getPluginData();
198
199 $currentWidgetData = $this->getSourceData();
200 if ( empty( $currentWidgetData ) || empty( $pluginData ) ) {
201 return;
202 }
203 LodashBasic::set( $pluginData, $this->settingPath, $currentWidgetData );
204
205 static::updatePluginDataContent( $pluginData );
206
207 //force this data to be recompiled
208 $this->data = null;
209 }
210
211 /**
212 * @param $pluginData array
213 */
214 public static function updatePluginDataContent( $plugin_data ) {
215 $plugin_data_post = static::getPluginDataPost();
216 if ( empty( $plugin_data_post ) || ( ! empty( $plugin_data_post ) && ! isset( $plugin_data_post->ID ) ) ) {
217 return;
218 }
219
220 $plugin_data_content_json = json_encode( $plugin_data, JSON_UNESCAPED_UNICODE );
221
222 $new_plugin_data_post = array(
223 'ID' => $plugin_data_post->ID,
224 'post_content' => $plugin_data_content_json,
225 );
226
227 wp_update_post( $new_plugin_data_post );
228
229 //update plugin data
230 static::$pluginData = static::getDataFromDatabase();
231 }
232
233 }
234