PluginProbe
WProofreader spell & grammar check plugin for WordPress / trunk
WProofreader spell & grammar check plugin for WordPress vtrunk
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / includes / class-wproofreader-config-builder.php

class-wproofreader-config-builder.php in WProofreader spell & grammar check plugin for WordPress trunk, at includes/class-wproofreader-config-builder.php

122 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Builds data passed from WordPress to the WProofreader JavaScript bundle.
8 */
9 class WProofreader_Config_Builder {
10
11 /** @var WProofreader */
12 private $plugin;
13
14 /**
15 * @param WProofreader $plugin Plugin facade.
16 */
17 public function __construct( WProofreader $plugin ) {
18 $this->plugin = $plugin;
19 }
20
21 /**
22 * Build localization payload for vendor scripts.
23 *
24 * @param array $overrides Config overrides.
25 * @return array
26 */
27 public function build( array $overrides = array() ): array {
28 $is_free = $this->plugin->is_free_edition();
29 $is_badge_enabled = ( $this->plugin->get_badge_button_option() === WProofreader::DEFAULT_BADGE_TOGGLE_OPTION );
30 $base_config = array(
31 'key_for_proofreader' => $this->plugin->get_customer_id(),
32 'slang' => $this->plugin->get_language(),
33 'settingsSections' => $is_free
34 ? array( 'dictionaries', 'languages', 'general', 'options' )
35 : array( 'options', 'languages', 'dictionaries', 'about', 'general' ),
36 'enableGrammar' => ! $is_free,
37 'aiWritingAssistant' => ! $is_free,
38 'enableBadgeButton' => $is_badge_enabled,
39 'globalBadge' => true,
40 'compactBadge' => true,
41 'autocomplete' => false,
42 'disableOptionsStorage' => $is_free ? array( 'autocomplete' ) : array(),
43 'disableDictionariesPreferences' => $is_free,
44 );
45
46 if ( $is_free ) {
47 $base_config['generalOptions'] = array( 'spellingSuggestions', 'grammarSuggestions', 'styleGuideSuggestions', 'autocorrect' );
48 }
49
50 $config = wp_parse_args( $overrides, $base_config );
51
52 if ( ! is_array( $config['disableOptionsStorage'] ) ) {
53 $config['disableOptionsStorage'] = array();
54 }
55
56 return $this->normalize_booleans( $config );
57 }
58
59 /**
60 * Build config for the settings-page Web API instance.
61 *
62 * @param string $ajax_nonce AJAX nonce.
63 * @return array
64 */
65 public function build_instance( string $ajax_nonce ): array {
66 return $this->normalize_booleans(
67 array(
68 'key_for_proofreader' => $this->plugin->get_customer_id(),
69 'slang' => $this->plugin->get_language(),
70 'ajax_nonce' => $ajax_nonce,
71 'enableGrammar' => ! $this->plugin->is_free_edition(),
72 )
73 );
74 }
75
76 /**
77 * Shared WSC service configuration. Filterable via `wsc_service_config`.
78 *
79 * @return array
80 */
81 public function service_config(): array {
82 $defaults = array(
83 'serviceProtocol' => 'https',
84 'serviceHost' => 'svc.webspellchecker.net',
85 'servicePath' => 'api',
86 'servicePort' => '443',
87 'bundleUrl' => 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js',
88 );
89
90 $filtered = apply_filters( 'wsc_service_config', $defaults );
91
92 return wp_parse_args( is_array( $filtered ) ? $filtered : array(), $defaults );
93 }
94
95 /**
96 * The remote bundle URL for script registration.
97 *
98 * @return string
99 */
100 public function bundle_url(): string {
101 $service_config = $this->service_config();
102
103 return esc_url_raw( (string) $service_config['bundleUrl'] );
104 }
105
106 /**
107 * Keep legacy JS compatibility by passing booleans as "true"/"false" strings.
108 *
109 * @param array $config Raw config.
110 * @return array
111 */
112 private function normalize_booleans( array $config ): array {
113 foreach ( $config as $key => $value ) {
114 if ( is_bool( $value ) ) {
115 $config[ $key ] = $value ? 'true' : 'false';
116 }
117 }
118
119 return $config;
120 }
121 }
122