forked from chetmac/airpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairpress.php
457 lines (352 loc) · 10.8 KB
/
airpress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/*
Plugin Name: Airpress
Plugin URI: http://chetmac.com/airpress
Description: Extend Wordpress Posts, Pages, and Custom Fields with data from remote Airtable records.
Version: 1.1.55
Author: Chester McLaughlin
Author URI: http://chetmac.com
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
$airpress_version = "1.1.55";
if ( ! defined( 'WPINC' ) ) {
die;
}
// Create a helper function for easy SDK access.
function air_fs() {
global $air_fs;
if ( ! isset( $air_fs ) ) {
// Include Freemius SDK.
require_once dirname(__FILE__) . '/freemius/start.php';
$air_fs = fs_dynamic_init( array(
'id' => '2775',
'slug' => 'airpress',
'type' => 'plugin',
'public_key' => 'pk_366237d3b7511bd7494423c0df287',
'is_premium' => false,
'has_addons' => false,
'has_paid_plans' => false,
'menu' => array(
'slug' => 'airpress_settings',
'first-path' => 'admin.php?page=airpress_cx',
),
) );
}
return $air_fs;
}
// Init Freemius.
air_fs();
// Signal that SDK was initiated.
do_action( 'air_fs_loaded' );
function air_fs_custom_connect_message_on_update(
$message,
$user_first_name,
$plugin_title,
$user_login,
$site_link,
$freemius_link
) {
return sprintf(
__( 'Hey %1$s' ) . ',<br>' .
__( 'I\'m so pleased you\'re using %2$s! Please help me improve it! If you opt-in, some data about your usage of %2$s will be sent to %5$s. If you skip this, that\'s okay! %2$s will still work just fine.', 'airpress' ),
$user_first_name,
'<b>' . $plugin_title . '</b>',
'<b>' . $user_login . '</b>',
$site_link,
$freemius_link
);
}
air_fs()->add_filter('connect_message_on_update', 'air_fs_custom_connect_message_on_update', 10, 6);
require_once("lib/chetmac/Airpress.php");
require_once("lib/chetmac/AirpressConnect.php");
require_once("lib/chetmac/AirpressQuery.php");
require_once("lib/chetmac/AirpressRecord.php");
require_once("lib/chetmac/AirpressCollection.php");
require_once("lib/chetmac/AirpressVirtualFields.php");
require_once("lib/chetmac/AirpressVirtualPosts.php");
if (is_admin()){
require_once("lib/chetmac/AirpressAdmin.php");
require_once("lib/chetmac/AirpressVirtualPostsAdmin.php");
require_once("lib/chetmac/AirpressVirtualFieldsAdmin.php");
}
$airpress = new Airpress();
add_action( 'plugins_loaded', array($airpress,'init') );
if ( is_admin()){
add_action( 'plugins_loaded', array($airpress,"check_for_update") );
}
if ( ! class_exists("Parsedown")){
require_once("lib/erusev/Parsedown.php");
}
if (!isset($parsedown)){
$parsedown = new Parsedown();
$parsedown->setBreaksEnabled(true);
}
// add ajax handle which will act as trigger
add_action( 'wp_ajax_nopriv_airpress_deferred', 'airpress_execute_deferred_queries' );
add_action( 'wp_ajax_airpress_deferred', 'airpress_execute_deferred_queries' );
function airpress_get_current_record(){
global $airpress, $post;
if ( is_airpress_collection($post->AirpressCollection) ){
if ( ! empty($airpress->loopscope) ){
return $airpress->loopscope[0];
} else {
return $post->AirpressCollection[0];
}
}
}
function airpress_execute_deferred_queries() {
global $airpress;
$airpress->run_deferred_queries($_GET["stash_key"]);
wp_send_json_success(array());
}
function is_airpress_attachment($input=null){
return is_array($input) && isset($input[0]["url"]);
}
function is_airpress_collection($input=null){
if (isset($input) && is_object($input) && get_class($input) == "AirpressCollection"){
return true;
}
return false;
}
function is_airpress_empty($input=null){
if (!is_airpress_collection($input) || !isset($input[0]) || !is_airpress_record($input[0])){
return true;
}
return false;
}
function is_airpress_record($input=null){
if (isset($input) && is_object($input) && get_class($input) == "AirpressRecord"){
return true;
}
return false;
}
function get_airpress_configs($option_group,$run_filter=true){
$configs = array();
$id = 0;
while ($config = get_airpress_config($option_group,$id) ){
$config["id"] = $id; // never assumed the ID will be the same
$configs[] = $config;
$id++;
}
if (count($configs) == 1 && $configs[0]["name"] == "New Configuration"){
$configs = array();
}
if ($run_filter){
$configs = apply_filters( 'airpress_configs', $configs, $option_group );
}
return $configs;
}
function get_airpress_config($option_group, $id){
if ( !is_int($id) ){
$configs = get_airpress_configs($option_group);
foreach($configs as $config){
if ($id == $config["name"]){
break;
}
}
} else {
$config = get_option($option_group.$id);
}
if (!$config)
return false;
return $config;
}
function delete_airpress_config($option_group, $id){
$options = get_airpress_configs($option_group,false);
foreach($options as $key => $option){
delete_option($option_group.$key);
}
unset($options[$id]);
$options = array_values($options);
foreach($options as $key => $option){
$option["id"] = $key;
add_option($option_group.$key,$option);
}
}
function set_airpress_config($option_group, $id, $config){
update_option($option_group.$id,$config);
}
function is_airpress_force_fresh($config=0){
if ( ! is_array($config) ){
$config = get_airpress_config("airpress_cx",$config);
}
$fresh_param = $config["fresh"];
if (isset($_GET[$fresh_param]) && $_GET[$fresh_param] == "true"){
return true;
} else {
return false;
}
}
function airpress_getArrayValue($array,$keys){
// for backwards compatibility
return airpress_getArrayValues($array,$keys);
}
function airpress_getArrayValues($array,$keys){
while(!empty($keys)){
$key = array_shift($keys);
if (isset($array[$key])){
$array = $array[$key];
} else {
// Maybe it's an array of arrays
$return_array = array();
foreach($array as $item){
if ( isset($item[$key]) ){
$return_array[] = $item[$key];
}
}
if ( ! empty($return_array) ){
$array = $return_array;
}
}
}
return $array;
}
function airpress_parse_template($template, $record, $replacementFields=null){
if ( is_null($replacementFields) ){
preg_match_all("/{{([^}]*)}}/", $template, $matches);
$replacementFields = array_unique($matches[1]);
}
foreach($replacementFields as $replacementField){
$keys = explode("|", $replacementField);
$field = array_shift($keys);
$replacementValue = "";
if ( strtolower($field) == "record_id" ){
if ( is_airpress_record($record)){
$replacementValue = $record->record_id();
} else {
airpress_debug(0,"Attempting to populate field $field on a non-populated record",$keys);
}
} else if ( ! is_airpress_empty( $record[$field] ) ){
// this means it IS an AirpressCollection with records
if (empty($keys)){
// this shouldn't really happen because we can't output a collection
// we should be looking INSIDE the collection, but can't since keys is empty
} else {
$replacementValue = implode(", ", $record[$field]->getFieldValues($keys) );
}
// This field is an array
} else if (is_array($record[$field]) ){
if (empty($keys)){
$replacementValue = implode(", ",$record[$field]);
} else {
$array = $record[$field];
while (!empty($keys)){
$key = array_shift($keys);
$array = $array[$key];
}
if (is_array($array)){
$replacementValue = implode(", ",$array);
} else {
$replacementValue = $array;
}
}
} else if (isset($record[$field])){
$replacementValue = $record[$field];
} else {
$replacementValue = "";
}
$template = str_replace("{{".$replacementField."}}", $replacementValue, $template);
}
return $template;
}
function airpress_flush_cache($all=false){
global $wpdb;
$to_delete = array();
$expirations = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_name LIKE "_transient_timeout_aprq_%" ORDER BY option_value ASC', OBJECT );
$exp = array();
foreach($expirations as $row){
$hash = str_replace("_transient_timeout_aprq_","",$row->option_name);
if (time() >= $row->option_value || $all === true ){
$to_delete[] = "_transient_timeout_aprq_".$hash;
$to_delete[] = "_transient_aprq_".$hash;
}
}
if (!empty($to_delete)){
$in = '"'.implode('","', $to_delete).'"';
$wpdb->get_results( 'DELETE FROM wp_options WHERE option_name IN ('.$in.')', OBJECT );
}
}
function airpress_debug($cx=0,$message=null,$object=null){
global $airpress;
if ( !isset($airpress)){
// somehow this happens with wp cli
return;
}
if ( is_null($message) ){
$message = $cx;
$cx = 0;
}
if ( ! is_array($cx) ){
$config = get_airpress_config("airpress_cx",$cx);
} else {
$config = $cx;
}
if ( isset($config["debug"]) ){
if ( $config["debug"] == 1 || $config["debug"] == 2 ){
$object_string = print_r($object,true);
$object_string = str_replace(substr($config["api_key"],3,-3),"*******",$object_string);
$object_string = str_replace(substr($config["app_id"],3,-3),"*******",$object_string);
if ( $h = @fopen($config["log"], "a") ){
fwrite($h, $message."\n");
if (isset($object)){
fwrite($h, "#########################################\n".$object_string."\n#########################################\n\n");
}
fclose($h);
}
}
if ( $config["debug"] == 1 || $config["debug"] == 3 ){
if ( ! is_null($object) ){
// ob_start();
// var_dump($object);
// $expanded = ob_get_clean();;
$airpress->debug_output .= "+ <a class='expander' href='#'>$message</a>";
$airpress->debug_output .= "<div class='expandable'>".print_r($object_string,true)."</div>";
//$airpress->debug_output .= "<div class='expandable'>$expanded</div>";
} else {
$airpress->debug_output .= $message;
}
$airpress->debug_output .= "<br><br>";
}
}
}
// Look at me being backwards compatible while
// working towards using namespaced functions.
if ( ! function_exists("is_cornerstone") ){
function is_cornerstone(){
return is_airpress_cornerstone();
}
}
function is_airpress_cornerstone(){
if (isset($_GET["action"]) && $_GET["action"] == "cs_render_element"){
// Cornerstone Element Render
return "render";
} else if (isset($_GET["cornerstone_preview"])){
// Cornerstone Admin Preview iFrame
return "preview";
} else if (isset($_GET["cornerstone"])){
// Cornerstone Admin
return "admin";
}
return false;
}
function is_airpress_elementor(){
if (isset($_POST["action"]) && $_POST["action"] == "elementor_ajax"){
// elementor Element Render
return "render";
}
return false;
}
function is_airpress_compatible_page_builder(){
if ( $mode = is_airpress_cornerstone() ){
return $mode;
}
if ( $mode = is_airpress_elementor() ){
return $mode;
}
return false;
}
/*
to do: Allow cacheImageFields via shortcodes
*/
?>