| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX; |
| 4 |
|
| 5 |
final class Config { |
| 6 |
private const DEFAULT_OPTIONS = [ |
| 7 |
'version' => null, |
| 8 |
'at' => ' [at] ', |
| 9 |
'dot' => ' [dot] ', |
| 10 |
'css_id' => '', |
| 11 |
'css_class' => '', |
| 12 |
'the_content' => 1, |
| 13 |
'the_meta_key' => 1, |
| 14 |
'the_excerpt' => 1, |
| 15 |
'comment_text' => 1, |
| 16 |
'widget_text' => 1, |
| 17 |
'java' => 1, |
| 18 |
'load_java' => 1, |
| 19 |
'opt_linktext' => 0, |
| 20 |
'autolink' => 1, |
| 21 |
'alt_linktext' => '', |
| 22 |
'alt_linkimage' => '', |
| 23 |
'http_linkimage_title' => '', |
| 24 |
'alt_linkimage_title' => '', |
| 25 |
'excludedIDs' => '', |
| 26 |
'metaBox' => 1, |
| 27 |
'alt_uploadedimage' => '0', |
| 28 |
'c2i_font' => null, |
| 29 |
'c2i_fontSize' => 10, |
| 30 |
'c2i_fontRGB' => '#000000', |
| 31 |
'echo' => 1, |
| 32 |
'filter' => ['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', 'widget_text'], |
| 33 |
'whiteList' => 'jpeg,jpg,png,gif', |
| 34 |
'disable_rss' => 1, // Disable CryptX in RSS feeds by default |
| 35 |
]; |
| 36 |
|
| 37 |
private array $options; |
| 38 |
private array $originalOptions; |
| 39 |
|
| 40 |
public function __construct(array $options = []) { |
| 41 |
$this->options = array_merge(self::DEFAULT_OPTIONS, $options); |
| 42 |
$this->originalOptions = $this->options; |
| 43 |
} |
| 44 |
|
| 45 |
public function getActiveFilters(): array { |
| 46 |
return array_filter($this->options['filter'], fn($filter) => |
| 47 |
isset($this->options[$filter]) && $this->options[$filter] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function isMetaBoxEnabled(): bool { |
| 52 |
return (bool) ($this->options['metaBox'] ?? false); |
| 53 |
} |
| 54 |
|
| 55 |
public function isAutolinkEnabled(): bool { |
| 56 |
return (bool) ($this->options['autolink'] ?? false); |
| 57 |
} |
| 58 |
|
| 59 |
public function getLinkTextOption(): int { |
| 60 |
return (int) ($this->options['opt_linktext'] ?? 0); |
| 61 |
} |
| 62 |
|
| 63 |
public function getFontSettings(): array { |
| 64 |
return [ |
| 65 |
'font' => $this->options['c2i_font'], |
| 66 |
'size' => (int) $this->options['c2i_fontSize'], |
| 67 |
'color' => $this->options['c2i_fontRGB'] |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
public function getCssSettings(): array { |
| 72 |
return [ |
| 73 |
'id' => $this->options['css_id'], |
| 74 |
'class' => $this->options['css_class'] |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
public function getEmailReplacements(): array { |
| 79 |
return [ |
| 80 |
'at' => $this->options['at'], |
| 81 |
'dot' => $this->options['dot'] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
public function getImageSettings(): array { |
| 86 |
return [ |
| 87 |
'url' => $this->options['alt_linkimage'], |
| 88 |
'title' => $this->options['http_linkimage_title'], |
| 89 |
'uploaded_id' => $this->options['alt_uploadedimage'], |
| 90 |
'uploaded_title' => $this->options['alt_linkimage_title'] |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
public function getExcludedIds(): array { |
| 95 |
$ids = $this->options['excludedIDs']; |
| 96 |
return $ids ? array_map('trim', explode(',', $ids)) : []; |
| 97 |
} |
| 98 |
|
| 99 |
public function getVersion(): ?string { |
| 100 |
return $this->options['version']; |
| 101 |
} |
| 102 |
|
| 103 |
public function updateFromShortcode(array $attributes, string $tag): void { |
| 104 |
$this->originalOptions = $this->options; |
| 105 |
$shortcodeOptions = shortcode_atts( |
| 106 |
$this->options, |
| 107 |
array_change_key_case($attributes, CASE_LOWER), |
| 108 |
$tag |
| 109 |
); |
| 110 |
$this->options = array_merge($this->options, $shortcodeOptions); |
| 111 |
} |
| 112 |
|
| 113 |
public function restoreOriginalOptions(): void { |
| 114 |
$this->options = $this->originalOptions; |
| 115 |
} |
| 116 |
|
| 117 |
public function save(): void { |
| 118 |
update_option('cryptX', $this->options); |
| 119 |
} |
| 120 |
|
| 121 |
public function update(array $newOptions): void |
| 122 |
{ |
| 123 |
// Convert checkbox values to integers |
| 124 |
foreach (['the_content', 'the_meta_key', 'the_excerpt', 'comment_text', |
| 125 |
'widget_text', 'autolink', 'metaBox', 'disable_rss'] as $key) { |
| 126 |
if (isset($newOptions[$key])) { |
| 127 |
$newOptions[$key] = (int)$newOptions[$key]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$this->options = array_merge($this->options, $newOptions); |
| 132 |
$this->save(); // Save immediately after update |
| 133 |
} |
| 134 |
|
| 135 |
public function get(string $key, $default = null) { |
| 136 |
return $this->options[$key] ?? $default; |
| 137 |
} |
| 138 |
|
| 139 |
public function has(string $key): bool { |
| 140 |
return isset($this->options[$key]); |
| 141 |
} |
| 142 |
|
| 143 |
public function getAll(): array { |
| 144 |
return $this->options; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Resets all options to their default values |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function reset(): void |
| 153 |
{ |
| 154 |
$this->options = self::DEFAULT_OPTIONS; |
| 155 |
|
| 156 |
// Set version and default font |
| 157 |
$this->options['version'] = CRYPTX_VERSION; |
| 158 |
|
| 159 |
// Set default font if available |
| 160 |
$fontFiles = glob(CRYPTX_DIR_PATH . 'fonts/*.ttf'); |
| 161 |
if (!empty($fontFiles)) { |
| 162 |
$this->options['c2i_font'] = basename($fontFiles[0]); |
| 163 |
} |
| 164 |
|
| 165 |
$this->save(); |
| 166 |
} |
| 167 |
} |