| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) exit; ?> |
| 2 |
<?php |
| 3 |
|
| 4 |
/** |
| 5 |
***** DO NOT CALL ANY FUNCTIONS DIRECTLY FROM THIS FILE ****** |
| 6 |
* |
| 7 |
* This file will be loaded even before the framework is loaded |
| 8 |
* so the $app is not available here, only declare functions here. |
| 9 |
*/ |
| 10 |
|
| 11 |
$globalsDevFile = __DIR__ . '/globals_dev.php'; |
| 12 |
|
| 13 |
is_readable($globalsDevFile) && include $globalsDevFile; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function fluentCart($module = false) |
| 19 |
{ |
| 20 |
return \FluentCart\App\App::getInstance($module); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* |
| 25 |
* @return \FluentCart\App\Helpers\FluentCartUtilHelper |
| 26 |
*/ |
| 27 |
function fluentCartUtil() |
| 28 |
{ |
| 29 |
static $class; |
| 30 |
|
| 31 |
if (!$class) { |
| 32 |
$class = new \FluentCart\App\Helpers\FluentCartUtilHelper(); |
| 33 |
} |
| 34 |
|
| 35 |
return $class; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
function fluent_cart_success_log($title, $content, $otherInfo = []) |
| 40 |
{ |
| 41 |
fluent_cart_add_log($title, $content, 'success', $otherInfo); |
| 42 |
} |
| 43 |
|
| 44 |
function fluent_cart_error_log($title, $content, $otherInfo = []) |
| 45 |
{ |
| 46 |
if (!defined('FLUENT_CART_DEV_MODE') || !FLUENT_CART_DEV_MODE) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
fluent_cart_add_log($title, $content, 'error', $otherInfo); |
| 51 |
} |
| 52 |
|
| 53 |
function fluent_cart_warning_log($title, $content, $otherInfo = []) |
| 54 |
{ |
| 55 |
fluent_cart_add_log($title, $content, 'warning', $otherInfo); |
| 56 |
} |
| 57 |
|
| 58 |
function fluent_cart_add_log($title, $content, $logStatus = "info", $otherInfo = []) |
| 59 |
{ |
| 60 |
$user = wp_get_current_user(); |
| 61 |
$title = sanitize_text_field($title); |
| 62 |
|
| 63 |
if (!$content || !is_string($content)) { |
| 64 |
$content = ''; |
| 65 |
} |
| 66 |
|
| 67 |
$default = [ |
| 68 |
'title' => $title, |
| 69 |
'content' => wp_kses_post($content), |
| 70 |
'status' => sanitize_text_field($logStatus), // info, error, warning, success |
| 71 |
'log_type' => 'activity', // api |
| 72 |
'module_type' => '', // Namespace/path/ |
| 73 |
'module_id' => null, // module id |
| 74 |
'module_name' => 'order', // 'order', 'product', 'user', 'coupon', 'subscription', 'payment', 'refund', 'shipment', 'activity |
| 75 |
'user_id' => get_current_user_id() ?? 0, |
| 76 |
'created_by' => $user->exists() ? ($user->display_name ?: 'FCT-BOT') : 'FCT-BOT', |
| 77 |
]; |
| 78 |
|
| 79 |
$allowedModels = [ |
| 80 |
'order', |
| 81 |
'product', |
| 82 |
'productVariation', |
| 83 |
'user', |
| 84 |
'coupon', |
| 85 |
'subscription' |
| 86 |
]; |
| 87 |
|
| 88 |
$allowedModels = apply_filters('fluent_cart/logs/allowed_models', $allowedModels); |
| 89 |
|
| 90 |
$data = array_merge($default, $otherInfo); |
| 91 |
//sanitize data |
| 92 |
$data['user_id'] = intval($data['user_id']); |
| 93 |
$data['created_by'] = sanitize_text_field($data['created_by']); |
| 94 |
$data['module_id'] = intval($data['module_id']); |
| 95 |
$data['module_name'] = sanitize_text_field($data['module_name']); |
| 96 |
|
| 97 |
$moduleName = $data['module_name'] ?: ''; |
| 98 |
|
| 99 |
if (empty($data['module_type']) && !empty($data['module_name']) && in_array(strtolower($moduleName), $allowedModels)) { |
| 100 |
$data['module_type'] = 'FluentCart\\App\\Models\\' . ucfirst($data['module_name']); |
| 101 |
} |
| 102 |
|
| 103 |
$data['module_type'] = sanitize_text_field($data['module_type']); |
| 104 |
$data['log_type'] = sanitize_text_field($data['log_type']); |
| 105 |
|
| 106 |
$log = \FluentCart\Api\Resource\ActivityResource::create($data); |
| 107 |
|
| 108 |
if (\FluentCart\Framework\Support\Arr::get($otherInfo, 'trigger_admin_alert_email') === 'yes') { |
| 109 |
$mailingSettings = \FluentCart\App\Services\Email\EmailNotifications::getSettings(); |
| 110 |
$toEmail = \FluentCart\Framework\Support\Arr::get($mailingSettings, 'admin_email', ''); |
| 111 |
|
| 112 |
if ($toEmail) { |
| 113 |
$body = \FluentCart\App\App::make('view')->make('emails.general_template', [ |
| 114 |
'preHeader' => '', |
| 115 |
'emailBody' => $content, |
| 116 |
'header' => '', |
| 117 |
'emailFooter' => '' |
| 118 |
]); |
| 119 |
(new \FluentCart\App\Services\Email\Mailer($toEmail, 'FluentCart Log Alert: ' . $title, $body))->send(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $log; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* |
| 129 |
* @return \FluentCart\Api\Resource\FrontendResource\FluentMetaResource; |
| 130 |
* |
| 131 |
* param String $meta_key |
| 132 |
* param Boolean $default |
| 133 |
*/ |
| 134 |
function fluent_cart_get_option($meta_key, $default = false, $cache = true) |
| 135 |
{ |
| 136 |
static $caches = []; |
| 137 |
|
| 138 |
if (isset($caches[$meta_key]) && $cache) { |
| 139 |
return $caches[$meta_key]; |
| 140 |
} |
| 141 |
|
| 142 |
$exist = \FluentCart\App\Models\Meta::query() |
| 143 |
->where('meta_key', $meta_key) |
| 144 |
->where('object_type', 'option') |
| 145 |
->first(); |
| 146 |
|
| 147 |
if ($exist) { |
| 148 |
$caches[$meta_key] = $exist->meta_value; |
| 149 |
return $exist->meta_value; |
| 150 |
} |
| 151 |
|
| 152 |
$caches[$meta_key] = $default; |
| 153 |
|
| 154 |
return $default; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* |
| 159 |
* @return \FluentCart\App\Models\Meta|\FluentCart\Framework\Database\Orm\Builder; |
| 160 |
* |
| 161 |
* param $meta_key, $meta_value |
| 162 |
*/ |
| 163 |
function fluent_cart_update_option($meta_key, $meta_value) |
| 164 |
{ |
| 165 |
|
| 166 |
$exist = \FluentCart\App\Models\Meta::query() |
| 167 |
->where('meta_key', $meta_key) |
| 168 |
->where('object_type', 'option') |
| 169 |
->first(); |
| 170 |
|
| 171 |
if ($exist) { |
| 172 |
$exist->meta_value = $meta_value; |
| 173 |
$exist->save(); |
| 174 |
fluent_cart_get_option($meta_key, null, false); // reset cache |
| 175 |
return $exist; |
| 176 |
} |
| 177 |
|
| 178 |
$data = [ |
| 179 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 180 |
'meta_key' => $meta_key, |
| 181 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 182 |
'meta_value' => $meta_value, |
| 183 |
'object_type' => 'option' |
| 184 |
]; |
| 185 |
|
| 186 |
$result = \FluentCart\App\Models\Meta::query()->create($data); |
| 187 |
|
| 188 |
fluent_cart_get_option($meta_key, null, false); // reset cache |
| 189 |
|
| 190 |
return $result; |
| 191 |
} |
| 192 |
|
| 193 |
function fluent_cart_api() |
| 194 |
{ |
| 195 |
return \FluentCart\Api\FluentCartGeneralApi::getInstance(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
function fluent_cart_get_current_product() |
| 200 |
{ |
| 201 |
if (isset($GLOBALS['fct_product']) && $GLOBALS['fct_product'] instanceof \FluentCart\App\Models\Product) { |
| 202 |
return $GLOBALS['fct_product']; |
| 203 |
} |
| 204 |
|
| 205 |
// maybe it's too early |
| 206 |
$post = get_post(); |
| 207 |
if ($post instanceof WP_Post && $post->post_type === \FluentCart\App\CPT\FluentProducts::CPT_NAME) { |
| 208 |
return \FluentCart\App\Modules\Data\ProductDataSetup::getProductModel($post->ID); |
| 209 |
} |
| 210 |
|
| 211 |
return null; |
| 212 |
} |
| 213 |
|