| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 6 |
|
| 7 |
final class Utilities |
| 8 |
{ |
| 9 |
public static function isPro() |
| 10 |
{ |
| 11 |
// IMPORTANT: |
| 12 |
// For wp.org (free) distribution we treat "Pro" as "Pro addon is installed/active". |
| 13 |
// License enforcement must live inside the Pro addon, not in the free plugin. |
| 14 |
return class_exists('BitCode\\BitFormPro\\Plugin'); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Kept for backward compatibility where the free plugin needs to know if a Pro |
| 19 |
* license is marked active. Prefer checking license inside the Pro addon. |
| 20 |
*/ |
| 21 |
public static function isProLicensed(): bool |
| 22 |
{ |
| 23 |
if (!self::isPro()) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
if (class_exists('BitCode\\BitFormPro\\Core\\Util\\LicenseHelper')) { |
| 27 |
return \BitCode\BitFormPro\Core\Util\LicenseHelper::hasValidLicense(); |
| 28 |
} |
| 29 |
$integrateData = get_option('bitformpro_integrate_key_data'); |
| 30 |
|
| 31 |
return !empty($integrateData) |
| 32 |
&& is_array($integrateData) |
| 33 |
&& !empty($integrateData['status']) |
| 34 |
&& 'success' === $integrateData['status']; |
| 35 |
} |
| 36 |
|
| 37 |
public static function getRealPath($dir, $name) |
| 38 |
{ |
| 39 |
if (!isset($dir)) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
$directories = array_diff(scandir($dir), ['..', '.']); |
| 43 |
$name = strtolower($name); |
| 44 |
|
| 45 |
foreach ($directories as $directory) { |
| 46 |
$dirLower = strtolower($directory); |
| 47 |
if ($dirLower === $name) { |
| 48 |
return $directory; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
public static function getFileNameExceptExtension($filename) |
| 56 |
{ |
| 57 |
$path_parts = pathinfo($filename); |
| 58 |
return $path_parts['filename']; |
| 59 |
} |
| 60 |
|
| 61 |
public static function styleGenerator($styleObj, $important = false) |
| 62 |
{ |
| 63 |
if (is_array($styleObj)) { |
| 64 |
$styleObj = json_decode(wp_json_encode($styleObj)); |
| 65 |
} |
| 66 |
$important = $important ? '!important' : ''; |
| 67 |
$classes = array_keys((array) $styleObj); |
| 68 |
$css = ''; |
| 69 |
foreach ($classes as $class) { |
| 70 |
$css .= $class; |
| 71 |
$props = (array) $styleObj->{$class}; |
| 72 |
$propsKeys = array_keys($props); |
| 73 |
$styleObject = '{'; |
| 74 |
foreach ($propsKeys as $property) { |
| 75 |
$value = $props[$property]; |
| 76 |
if (empty($value)) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
if (is_object($value)) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
$styleObject .= "{$property}:{$value}{$important};"; |
| 83 |
} |
| 84 |
$styleObject = rtrim($styleObject, ';'); |
| 85 |
$styleObject .= '}'; |
| 86 |
$css .= $styleObject; |
| 87 |
} |
| 88 |
|
| 89 |
return $css; |
| 90 |
} |
| 91 |
|
| 92 |
public static function convertToCSS(array $styles): string |
| 93 |
{ |
| 94 |
$css = ''; |
| 95 |
|
| 96 |
foreach ($styles as $selector => $properties) { |
| 97 |
$css .= "$selector{"; |
| 98 |
|
| 99 |
// Check if the properties are an array, meaning it's a nested rule (e.g., media queries, keyframes) |
| 100 |
if (is_array($properties)) { |
| 101 |
// If it's a nested array (media query or keyframe), recursively handle it |
| 102 |
foreach ($properties as $property => $value) { |
| 103 |
if (is_array($value)) { |
| 104 |
// Recursively process nested arrays (media queries, keyframes, etc.) |
| 105 |
$css .= self::convertToCSS([$property => $value]); |
| 106 |
} else { |
| 107 |
// Regular CSS property |
| 108 |
$css .= "$property:$value;"; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$css .= '}'; |
| 114 |
} |
| 115 |
|
| 116 |
return $css; |
| 117 |
} |
| 118 |
|
| 119 |
public static function appendCSS($formId, $css, $mode = 'a') |
| 120 |
{ |
| 121 |
$fileName = ''; |
| 122 |
$path = ''; |
| 123 |
$path = 'form-styles'; |
| 124 |
$fileName = "bitform-{$formId}.css"; |
| 125 |
Helpers::saveFile($path, $fileName, $css, $mode); |
| 126 |
$fileName = "bitform-{$formId}-formid.css"; |
| 127 |
Helpers::saveFile($path, $fileName, $css, $mode); |
| 128 |
} |
| 129 |
|
| 130 |
public static function duplicateDbTable($oldTableName, $newTableName) |
| 131 |
{ |
| 132 |
global $wpdb; |
| 133 |
$oldTable = "{$wpdb->prefix}bitforms_{$oldTableName}"; |
| 134 |
$newTable = "{$wpdb->prefix}bitforms_{$newTableName}"; |
| 135 |
// Schema operation: table name interpolation only (no user input). $wpdb->prepare() cannot parameterize DDL statements. |
| 136 |
$wpdb->query("CREATE TABLE IF NOT EXISTS `{$newTable}` LIKE `{$oldTable}`"); |
| 137 |
$wpdb->query("INSERT INTO `{$newTable}` SELECT * FROM `{$oldTable}`"); |
| 138 |
} |
| 139 |
} |
| 140 |
|