| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings Page Class |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* WINP_Settings Class |
| 15 |
*/ |
| 16 |
class WINP_Settings { |
| 17 |
|
| 18 |
/** |
| 19 |
* Singleton instance |
| 20 |
* |
| 21 |
* @var WINP_Settings|null |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Private constructor to prevent direct instantiation |
| 27 |
*/ |
| 28 |
private function __construct() { |
| 29 |
add_action( 'admin_menu', [ $this, 'register_settings_page' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register dashboard page |
| 34 |
*/ |
| 35 |
public function register_settings_page(): void { |
| 36 |
$page_hook_suffix = add_submenu_page( |
| 37 |
'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE, |
| 38 |
__( 'Settings', 'insert-php' ), |
| 39 |
__( 'Settings', 'insert-php' ), |
| 40 |
'manage_options', |
| 41 |
'winp-settings', |
| 42 |
[ $this, 'render_settings_page' ], |
| 43 |
4 |
| 44 |
); |
| 45 |
|
| 46 |
add_action( "admin_print_scripts-$page_hook_suffix", [ $this, 'enqueue_options_assets' ] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Render dashboard page content |
| 51 |
*/ |
| 52 |
public function render_settings_page(): void { |
| 53 |
echo '<div id="woody-settings"></div>'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Load assets for option page. |
| 58 |
*/ |
| 59 |
public function enqueue_options_assets(): void { |
| 60 |
$asset_file = include WINP_PLUGIN_DIR . '/admin/assets/dashboard/build/index.asset.php'; |
| 61 |
|
| 62 |
wp_enqueue_style( |
| 63 |
'winp-dashboard-styles', |
| 64 |
WINP_PLUGIN_URL . '/admin/assets/dashboard/build/style-index.css', |
| 65 |
[], |
| 66 |
$asset_file['version'] |
| 67 |
); |
| 68 |
|
| 69 |
wp_enqueue_script( |
| 70 |
'winp-dashboard-scripts', |
| 71 |
WINP_PLUGIN_URL . '/admin/assets/dashboard/build/index.js', |
| 72 |
$asset_file['dependencies'], |
| 73 |
$asset_file['version'], |
| 74 |
true |
| 75 |
); |
| 76 |
|
| 77 |
wp_set_script_translations( 'winp-dashboard-scripts', 'insert-php' ); |
| 78 |
|
| 79 |
wp_localize_script( |
| 80 |
'winp-dashboard-scripts', |
| 81 |
'winpObjects', |
| 82 |
[ |
| 83 |
'api' => 'woody/v1', |
| 84 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 85 |
'nonce' => wp_create_nonce( 'winp_settings_nonce' ), |
| 86 |
'assetsUrl' => WINP_PLUGIN_URL . '/admin/assets/', |
| 87 |
'sections' => $this->get_sections(), |
| 88 |
'settings' => self::get_settings(), |
| 89 |
'links' => [ |
| 90 |
'upgrade' => tsdk_utmify( WINP_UPGRADE, 'settings_page', 'upgrade_link' ), |
| 91 |
'docs' => WINP_DOCS, |
| 92 |
'support' => WINP_SUPPORT, |
| 93 |
'orgSupport' => WINP_ORG_SUPPORT, |
| 94 |
'websiteDesign' => tsdk_utmify( 'https://themeisle.com/wordpress-website-design/', 'expert_help_card_woody_snippets', 'website_design' ), |
| 95 |
'speedOptimization' => tsdk_utmify( 'https://themeisle.com/wordpress-speed-optimization/', 'expert_help_card_woody_snippets', 'speed_optimization' ), |
| 96 |
'seoFoundation' => tsdk_utmify( 'https://themeisle.com/wordpress-seo-foundation/', 'expert_help_card_woody_snippets', 'seo_foundation' ), |
| 97 |
'siteMaintenance' => tsdk_utmify( 'https://themeisle.com/wordpress-maintenance/', 'expert_help_card_woody_snippets', 'site_maintenance' ), |
| 98 |
'hackedSiteRepair' => tsdk_utmify( 'https://themeisle.com/wordpress-hacked-site-repair/', 'expert_help_card_woody_snippets', 'hacked_site_repair' ), |
| 99 |
], |
| 100 |
'isProActive' => WINP_Plugin::app()->premium->is_pro_active(), |
| 101 |
'license' => [ |
| 102 |
'key' => apply_filters( 'product_woody_license_key', 'free' ), |
| 103 |
'status' => apply_filters( 'product_woody_license_status', false ), |
| 104 |
], |
| 105 |
'maxUploadSize' => size_format( apply_filters( 'import_upload_size_limit', wp_max_upload_size() ) ), |
| 106 |
'snippetData' => $this->get_snippet_data(), |
| 107 |
'isRTL' => is_rtl(), |
| 108 |
] |
| 109 |
); |
| 110 |
|
| 111 |
do_action( 'themeisle_internal_page', WINP_PLUGIN_SLUG, 'settings' ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get settings sections |
| 116 |
* |
| 117 |
* @return array<array<string, string>> |
| 118 |
*/ |
| 119 |
public function get_sections() { |
| 120 |
$sections = [ |
| 121 |
[ |
| 122 |
'id' => 'general', |
| 123 |
'title' => __( 'General', 'insert-php' ), |
| 124 |
], |
| 125 |
[ |
| 126 |
'id' => 'code_editor', |
| 127 |
'title' => __( 'Code Editor', 'insert-php' ), |
| 128 |
], |
| 129 |
]; |
| 130 |
|
| 131 |
$sections = apply_filters( 'wbcr/inp/settings/sections', $sections ); |
| 132 |
|
| 133 |
$sections[] = [ |
| 134 |
'id' => 'import', |
| 135 |
'title' => __( 'Import', 'insert-php' ), |
| 136 |
]; |
| 137 |
|
| 138 |
$sections[] = [ |
| 139 |
'id' => 'export', |
| 140 |
'title' => __( 'Export', 'insert-php' ), |
| 141 |
]; |
| 142 |
|
| 143 |
return $sections; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get settings options |
| 148 |
* |
| 149 |
* @return array<array<string, mixed>> |
| 150 |
*/ |
| 151 |
public static function get_settings() { |
| 152 |
$options = [ |
| 153 |
[ |
| 154 |
'title' => __( 'Auto-activate New Snippets', 'insert-php' ), |
| 155 |
'hint' => __( 'Automatically activate snippets when creating or updating them.', 'insert-php' ), |
| 156 |
'name' => 'activate_by_default', |
| 157 |
'type' => 'checkbox', |
| 158 |
'section' => 'general', |
| 159 |
'value' => get_option( 'wbcr_inp_activate_by_default', true ), |
| 160 |
'default' => true, |
| 161 |
], |
| 162 |
[ |
| 163 |
'title' => __( 'Preserve Special Characters', 'insert-php' ), |
| 164 |
'hint' => __( 'Preserve HTML entities without converting them.', 'insert-php' ), |
| 165 |
'name' => 'keep_html_entities', |
| 166 |
'type' => 'checkbox', |
| 167 |
'section' => 'general', |
| 168 |
'value' => get_option( 'wbcr_inp_keep_html_entities' ), |
| 169 |
'default' => false, |
| 170 |
], |
| 171 |
[ |
| 172 |
'title' => __( 'Execute Shortcodes in Snippets', 'insert-php' ), |
| 173 |
'hint' => __( 'Process shortcodes within snippets.', 'insert-php' ), |
| 174 |
'name' => 'execute_shortcode', |
| 175 |
'type' => 'checkbox', |
| 176 |
'section' => 'general', |
| 177 |
'value' => get_option( 'wbcr_inp_execute_shortcode' ), |
| 178 |
'default' => false, |
| 179 |
], |
| 180 |
[ |
| 181 |
'title' => __( 'Enable Error Email Notifications', 'insert-php' ), |
| 182 |
'hint' => __( 'Email notifications for snippet errors.', 'insert-php' ), |
| 183 |
'name' => 'error_email_enabled', |
| 184 |
'type' => 'checkbox', |
| 185 |
'section' => 'general', |
| 186 |
'value' => get_option( 'wbcr_inp_error_email_enabled' ), |
| 187 |
'default' => false, |
| 188 |
], |
| 189 |
[ |
| 190 |
'title' => __( 'Error Notification Email', 'insert-php' ), |
| 191 |
'hint' => __( 'Email for error notifications. Defaults to admin email.', 'insert-php' ), |
| 192 |
'name' => 'error_email_address', |
| 193 |
'type' => 'email', |
| 194 |
'section' => 'general', |
| 195 |
'value' => get_option( 'wbcr_inp_error_email_address', get_option( 'admin_email' ) ), |
| 196 |
'default' => get_option( 'admin_email' ), |
| 197 |
'placeholder' => get_option( 'admin_email' ), |
| 198 |
'visibility' => [ |
| 199 |
'field' => 'error_email_enabled', |
| 200 |
'value' => true, |
| 201 |
'type' => 'equals', |
| 202 |
], |
| 203 |
], |
| 204 |
[ |
| 205 |
'title' => __( 'Delete All Data on Uninstall', 'insert-php' ), |
| 206 |
'hint' => __( 'Delete all data when uninstalling.', 'insert-php' ), |
| 207 |
'name' => 'complete_uninstall', |
| 208 |
'type' => 'checkbox', |
| 209 |
'section' => 'general', |
| 210 |
'value' => get_option( 'wbcr_inp_complete_uninstall' ), |
| 211 |
'default' => false, |
| 212 |
], |
| 213 |
[ |
| 214 |
'title' => __( 'Code Style', 'insert-php' ), |
| 215 |
'hint' => __( 'Choose a color theme for the code editor.', 'insert-php' ), |
| 216 |
'name' => 'code_editor_theme', |
| 217 |
'type' => 'dropdown', |
| 218 |
'section' => 'code_editor', |
| 219 |
'data' => self::get_available_themes(), |
| 220 |
'value' => get_option( 'wbcr_inp_code_editor_theme', 'default' ), |
| 221 |
'default' => 'default', |
| 222 |
], |
| 223 |
[ |
| 224 |
'title' => __( 'Indent With Tabs', 'insert-php' ), |
| 225 |
'hint' => __( 'Use tabs instead of spaces for indentation.', 'insert-php' ), |
| 226 |
'name' => 'code_editor_indent_with_tabs', |
| 227 |
'type' => 'checkbox', |
| 228 |
'section' => 'code_editor', |
| 229 |
'value' => get_option( 'wbcr_inp_code_editor_indent_with_tabs' ), |
| 230 |
'default' => false, |
| 231 |
], |
| 232 |
[ |
| 233 |
'title' => __( 'Tab Size', 'insert-php' ), |
| 234 |
'hint' => __( 'Number of spaces per Tab key press.', 'insert-php' ), |
| 235 |
'name' => 'code_editor_tab_size', |
| 236 |
'type' => 'integer', |
| 237 |
'section' => 'code_editor', |
| 238 |
'value' => get_option( 'wbcr_inp_code_editor_tab_size', 4 ), |
| 239 |
'default' => 4, |
| 240 |
], |
| 241 |
[ |
| 242 |
'title' => __( 'Indent Unit', 'insert-php' ), |
| 243 |
'hint' => __( 'Define the number of spaces used for each indentation level in code blocks.', 'insert-php' ), |
| 244 |
'name' => 'code_editor_indent_unit', |
| 245 |
'type' => 'integer', |
| 246 |
'section' => 'code_editor', |
| 247 |
'value' => get_option( 'wbcr_inp_code_editor_indent_unit', 4 ), |
| 248 |
'default' => 4, |
| 249 |
], |
| 250 |
[ |
| 251 |
'title' => __( 'Wrap Lines', 'insert-php' ), |
| 252 |
'hint' => __( 'Wrap long lines to fit the editor width.', 'insert-php' ), |
| 253 |
'name' => 'code_editor_wrap_lines', |
| 254 |
'type' => 'checkbox', |
| 255 |
'section' => 'code_editor', |
| 256 |
'value' => get_option( 'wbcr_inp_code_editor_wrap_lines', 1 ), |
| 257 |
'default' => true, |
| 258 |
], |
| 259 |
[ |
| 260 |
'title' => __( 'Line Numbers', 'insert-php' ), |
| 261 |
'hint' => __( 'Show line numbers in the editor.', 'insert-php' ), |
| 262 |
'name' => 'code_editor_line_numbers', |
| 263 |
'type' => 'checkbox', |
| 264 |
'section' => 'code_editor', |
| 265 |
'value' => get_option( 'wbcr_inp_code_editor_line_numbers', 1 ), |
| 266 |
'default' => true, |
| 267 |
], |
| 268 |
[ |
| 269 |
'title' => __( 'Auto Close Brackets', 'insert-php' ), |
| 270 |
'hint' => __( 'Auto-close brackets and quotes while typing.', 'insert-php' ), |
| 271 |
'name' => 'code_editor_auto_close_brackets', |
| 272 |
'type' => 'checkbox', |
| 273 |
'section' => 'code_editor', |
| 274 |
'value' => get_option( 'wbcr_inp_code_editor_auto_close_brackets', 1 ), |
| 275 |
'default' => true, |
| 276 |
], |
| 277 |
[ |
| 278 |
'title' => __( 'Highlight Selection Matches', 'insert-php' ), |
| 279 |
'hint' => __( 'Highlight all matches of selected text.', 'insert-php' ), |
| 280 |
'name' => 'code_editor_highlight_selection_matches', |
| 281 |
'type' => 'checkbox', |
| 282 |
'section' => 'code_editor', |
| 283 |
'value' => get_option( 'wbcr_inp_code_editor_highlight_selection_matches' ), |
| 284 |
'default' => false, |
| 285 |
], |
| 286 |
]; |
| 287 |
|
| 288 |
$options = apply_filters( 'wbcr/inp/settings/options', $options ); |
| 289 |
|
| 290 |
return $options; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Retrieve a list of the available CodeMirror themes |
| 295 |
* |
| 296 |
* @return array<array<string, mixed>> the available themes. |
| 297 |
*/ |
| 298 |
public static function get_available_themes() { |
| 299 |
$themes = null; |
| 300 |
|
| 301 |
$themes = []; |
| 302 |
$themes_dir = WINP_PLUGIN_DIR . '/admin/assets/css/cmthemes/'; |
| 303 |
$theme_files = glob( $themes_dir . '*.css' ); |
| 304 |
|
| 305 |
if ( is_array( $theme_files ) ) { |
| 306 |
foreach ( $theme_files as $theme ) { |
| 307 |
$theme = str_replace( $themes_dir, '', $theme ); |
| 308 |
$theme = str_replace( '.css', '', $theme ); |
| 309 |
$themes[] = [ |
| 310 |
'label' => $theme, |
| 311 |
'value' => $theme, |
| 312 |
]; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
array_unshift( |
| 317 |
$themes, |
| 318 |
[ |
| 319 |
'label' => 'default', |
| 320 |
'value' => 'default', |
| 321 |
] |
| 322 |
); |
| 323 |
|
| 324 |
return $themes; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Get snippet data |
| 329 |
* |
| 330 |
* @return array<string, array<array<string, mixed>>> |
| 331 |
*/ |
| 332 |
private function get_snippet_data() { |
| 333 |
$results = []; |
| 334 |
$types = []; |
| 335 |
$tags = []; |
| 336 |
|
| 337 |
$snippets = get_posts( |
| 338 |
[ |
| 339 |
'post_type' => WINP_SNIPPETS_POST_TYPE, |
| 340 |
'post_status' => 'publish', |
| 341 |
'numberposts' => - 1, |
| 342 |
] |
| 343 |
); |
| 344 |
|
| 345 |
if ( ! empty( $snippets ) ) { |
| 346 |
foreach ( (array) $snippets as $snippet ) { |
| 347 |
$snippet_type = WINP_Helper::get_snippet_type( $snippet->ID ); |
| 348 |
if ( ! isset( $types[ $snippet_type ] ) ) { |
| 349 |
$types[ $snippet_type ] = 1; |
| 350 |
} else { |
| 351 |
++$types[ $snippet_type ]; |
| 352 |
} |
| 353 |
|
| 354 |
$terms = wp_get_post_terms( $snippet->ID, WINP_SNIPPETS_TAXONOMY ); |
| 355 |
|
| 356 |
if ( ! empty( $terms ) ) { |
| 357 |
foreach ( (array) $terms as $snippet_tag ) { |
| 358 |
if ( ! isset( $tags[ $snippet_tag->slug ] ) ) { |
| 359 |
$tags[ $snippet_tag->slug ] = 1; |
| 360 |
} else { |
| 361 |
++$tags[ $snippet_tag->slug ]; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
foreach ( $types as $snippet_type => $count ) { |
| 368 |
$results['types'][] = [ |
| 369 |
'value' => $snippet_type, |
| 370 |
'label' => $snippet_type, |
| 371 |
'count' => $count, |
| 372 |
]; |
| 373 |
} |
| 374 |
|
| 375 |
foreach ( $tags as $tag => $count ) { |
| 376 |
$results['tags'][] = [ |
| 377 |
'value' => $tag, |
| 378 |
'label' => $tag, |
| 379 |
'count' => $count, |
| 380 |
]; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return $results; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Prevent cloning of the instance |
| 389 |
* |
| 390 |
* @throws \Exception An exception when trying to clone the instance. |
| 391 |
*/ |
| 392 |
private function __clone() { |
| 393 |
throw new \Exception( 'Cannot clone singleton' ); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Prevent unserialization of the instance |
| 398 |
* |
| 399 |
* @throws \Exception An exception when trying to unserialize the instance. |
| 400 |
*/ |
| 401 |
public function __wakeup() { |
| 402 |
throw new \Exception( 'Cannot unserialize singleton' ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Get singleton instance |
| 407 |
* |
| 408 |
* @return WINP_Settings |
| 409 |
*/ |
| 410 |
public static function get_instance() { |
| 411 |
if ( null === self::$instance ) { |
| 412 |
self::$instance = new self(); |
| 413 |
} |
| 414 |
return self::$instance; |
| 415 |
} |
| 416 |
} |
| 417 |
|