PluginProbe
Civist – Petitions and Fundraising / trunk
Civist – Petitions and Fundraising vtrunk
8.0.3 8.0.2 8.0.0 trunk
civist / class-civist-scripts.php

class-civist-scripts.php in Civist – Petitions and Fundraising trunk, at class-civist-scripts.php

298 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The plugin scripts related functionality
4 *
5 * @package civist
6 */
7
8 /**
9 * Handles the plugin scripts' registration.
10 */
11 class Civist_Scripts {
12 /**
13 * The plugin name.
14 *
15 * @var string
16 */
17 private $plugin_name;
18 /**
19 * The plugin file.
20 *
21 * @var string
22 */
23 private $plugin_file;
24 /**
25 * The plugin slug.
26 *
27 * @var string
28 */
29 private $plugin_slug;
30 /**
31 * The id of the media button in the post/page editor.
32 *
33 * @var string
34 */
35 private $editor_button_id;
36 /**
37 * The plugin jwt manager.
38 *
39 * @var Civist_Jwt_Manager
40 */
41 /**
42 * The plugin settings manager.
43 *
44 * @var Civist_Settings_Manager
45 */
46 private $settings_manager;
47 /**
48 * The widget supported languages.
49 *
50 * @var array
51 */
52 private $widget_supported_languages;
53 /**
54 * Enforces HTTPS
55 *
56 * @var bool
57 */
58 private $enforce_https;
59 /**
60 * The plugin jwt manager.
61 *
62 * @var ?Civist_Jwt
63 */
64 private $jwt;
65
66 /**
67 * The Civist_Admin class constructor
68 *
69 * @param string $plugin_name The name of the plugin.
70 * @param string $plugin_file The file of the plugin.
71 * @param string $plugin_slug The slug of the plugin.
72 * @param Civist_Settings_Manager $settings_manager The plugin settings manager.
73 * @param array $widget_supported_languages The widget supported languages.
74 * @param bool $enforce_https Enforces HTTPS.
75 * @param Civist_Jwt $jwt The plugin jwt manager.
76 */
77 public function __construct( $plugin_name, $plugin_file, $plugin_slug, Civist_Settings_Manager $settings_manager, $widget_supported_languages, $enforce_https, ?Civist_Jwt $jwt = null ) {
78 $this->plugin_name = $plugin_name;
79 $this->plugin_file = $plugin_file;
80 $this->plugin_slug = $plugin_slug;
81 $this->settings_manager = $settings_manager;
82 $this->jwt = $jwt;
83 $this->widget_supported_languages = $widget_supported_languages;
84 $this->enforce_https = $enforce_https;
85 }
86
87 /**
88 * Register and enqueue the civist icon font styles
89 *
90 * @param string $webpack_files The files to inject.
91 */
92 public function enqueue_civist_icon_font( $webpack_files ) {
93 $plugin_dir_url = plugin_dir_url( __FILE__ );
94 wp_enqueue_style( 'civist_font', $plugin_dir_url . $webpack_files->civist2017->css[0], array(), $webpack_files->civist2017->hash );
95 }
96
97 /**
98 * Register and enqueue the plugin scripts for the plugin registration page
99 *
100 * @param string $webpack_files The files to inject.
101 */
102 public function enqueue_registration_scripts( $webpack_files ) {
103 $plugin_dir_url = plugin_dir_url( __FILE__ );
104 $name = 'civist_script';
105 wp_register_script( $name, $plugin_dir_url . $webpack_files->registration->entry, array(), $webpack_files->registration->hash, true );
106 $params = array_merge(
107 $this->get_management_configuration(),
108 array(
109 'registrationNonce' => wp_create_nonce( $this->settings_manager->get_nonce_action() ),
110 'registrationUrl' => $this->settings_manager->get_option_by_name( 'registration_url' ),
111 'settingsAjaxAction' => $this->settings_manager->get_client_action_name(),
112
113 )
114 );
115 wp_localize_script( $name, 'civist', $params );
116 $this->inject_vendor( $plugin_dir_url, $webpack_files );
117 wp_enqueue_script( $name );
118 }
119
120 /**
121 * Register and enqueue the plugin scripts for the plugins page
122 *
123 * @param string $webpack_files The files to inject.
124 * @param string $reconnect_link_id The id of the reconnect plugin action link element.
125 */
126 public function enqueue_plugins_scripts( $webpack_files, $reconnect_link_id ) {
127 $plugin_dir_url = plugin_dir_url( __FILE__ );
128 $name = 'civist_script';
129 wp_enqueue_media();
130 wp_register_script( $name, $plugin_dir_url . $webpack_files->plugins->entry, array(), $webpack_files->plugins->hash, true );
131 $params = array_merge(
132 $this->get_management_configuration(),
133 array(
134 'apiKeyId' => $this->settings_manager->get_option_by_name( 'api_key_id' ),
135 'reconnectLinkId' => $reconnect_link_id,
136 'reconnectNonce' => wp_create_nonce( $this->settings_manager->get_nonce_action() ),
137 'settingsAjaxAction' => $this->settings_manager->get_client_action_name(),
138
139 )
140 );
141 wp_localize_script( $name, 'civist', $params );
142 $this->inject_vendor( $plugin_dir_url, $webpack_files );
143 wp_enqueue_script( $name );
144 }
145
146 /**
147 * Register and enqueue the plugin scripts for the management pages
148 *
149 * @param string $webpack_files The files to inject.
150 */
151 public function enqueue_manager_scripts( $webpack_files ) {
152 $plugin_dir_url = plugin_dir_url( __FILE__ );
153 $name = 'civist_script';
154 wp_enqueue_media();
155 wp_register_script( $name, $plugin_dir_url . $webpack_files->manager->entry, array(), $webpack_files->manager->hash, true );
156 wp_enqueue_style( 'wp-color-picker' );
157 wp_enqueue_script( 'wp-color-picker' );
158 wp_editor(
159 '',
160 'civist',
161 array(
162 'editor_height' => 0,
163 'quicktags' => false,
164 'media_buttons' => false,
165 'tinymce' => false,
166 'editor_class' => 'hidden',
167 )
168 );
169 $this->inject_vendor( $plugin_dir_url, $webpack_files );
170 $params = $this->get_management_configuration();
171 wp_localize_script( $name, 'civist', $params );
172 wp_enqueue_script( $name );
173 }
174
175 /**
176 * Register and enqueue the plugin scripts for the plugin settings page
177 *
178 * @param string $webpack_files The files to inject.
179 */
180 public function enqueue_settings_scripts( $webpack_files ) {
181 $plugin_dir_url = plugin_dir_url( __FILE__ );
182 $name = 'civist-script';
183 $params = $this->get_management_configuration();
184 wp_enqueue_style( 'wp-color-picker' );
185 wp_enqueue_script( 'wp-color-picker' );
186 wp_enqueue_style( 'media-views' );
187 wp_register_script( $name, $plugin_dir_url . $webpack_files->settings->entry, array(), $webpack_files->settings->hash, true );
188 $this->inject_vendor( $plugin_dir_url, $webpack_files );
189 wp_localize_script( $name, 'civist', $params );
190 wp_enqueue_script( $name );
191 }
192
193 /**
194 * Register and enqueue the plugin scripts for the post and page editor
195 *
196 * @param string $webpack_files The files to inject.
197 * @param string $open_editor_modal_callback The name of the callback registered on window that opens the embed modal.
198 * @param string $embed_block_name The name of the embed block.
199 * @param string $form_block_name The name of the form block.
200 * @param string $progress_block_name The name of the progress block.
201 * @param string $civist_block_name The name of the civist block.
202 */
203 public function enqueue_editor_scripts( $webpack_files, $open_editor_modal_callback, $embed_block_name, $form_block_name, $progress_block_name, $civist_block_name ) {
204 $name = 'civist-script';
205 $plugin_dir_url = plugin_dir_url( __FILE__ );
206 $params = array_merge(
207 $this->get_management_configuration(),
208 array(
209 'openEditorModalCallback' => $open_editor_modal_callback,
210 'embedBlockName' => $embed_block_name,
211 'formBlockName' => $form_block_name,
212 'progressBlockName' => $progress_block_name,
213 'civistBlockName' => $civist_block_name,
214 )
215 );
216
217 wp_enqueue_media();
218 wp_register_script( $name, $plugin_dir_url . $webpack_files->editor->entry, array(), $webpack_files->editor->hash, true );
219 $this->inject_vendor( $plugin_dir_url, $webpack_files );
220 wp_localize_script( $name, 'civist', $params );
221 wp_enqueue_script( $name );
222 }
223
224 /**
225 * Builds the params that should be injected into js files under `civist_public` global
226 *
227 * @return array
228 */
229 public function get_public_configuration() {
230 global $content_width;
231
232 return array(
233 'apiUrl' => $this->settings_manager->get_option_by_name( 'api_url' ),
234 'version' => $this->settings_manager->get_option_by_name( 'version' ),
235 'contentWidth' => $content_width,
236 'widgetSupportedLanguages' => $this->widget_supported_languages,
237 );
238 }
239
240 /**
241 * Builds the params that should be injected into js files under `civist` global
242 *
243 * @return array
244 */
245 public function get_management_configuration() {
246 if ( ! is_admin() ) {
247 return $this->get_public_configuration();
248 }
249 global $wp_version;
250 global $content_width;
251
252 $locale = get_locale();
253 $admin_email = get_option( 'admin_email' );
254 $plugin_dir_url = plugin_dir_url( __FILE__ );
255 $params = array(
256 'adminAjaxUrl' => admin_url( 'admin-ajax.php' ),
257 'adminEmail' => $admin_email,
258 'adminUrl' => admin_url( 'admin.php' ),
259 'apiUrl' => $this->settings_manager->get_option_by_name( 'api_url' ),
260 'apiKeyId' => $this->settings_manager->get_option_by_name( 'api_key_id' ),
261 'contentWidth' => $content_width,
262 'enforceHttps' => $this->enforce_https ? 'enforce' : 'allow',
263 'locale' => $locale,
264 'pluginUrl' => $plugin_dir_url,
265 'pluginDashboardUrl' => admin_url( 'admin.php' ) . '?page=' . $this->plugin_slug,
266 'pluginSettingsUrl' => admin_url( 'options-general.php' ) . '?page=' . $this->plugin_slug . '-settings',
267 'pluginSlug' => $this->plugin_slug,
268 'siteUrl' => get_site_url(),
269 'version' => $this->settings_manager->get_option_by_name( 'version' ),
270 'widgetUrl' => $this->settings_manager->get_option_by_name( 'widget_url' ),
271 'geoipUrl' => $this->settings_manager->get_option_by_name( 'geoip_url' ),
272 'widgetSupportedLanguages' => $this->widget_supported_languages,
273 'wpVersion' => $wp_version,
274 );
275 if ( null !== $this->jwt ) {
276 $params = array_merge(
277 $params,
278 array(
279 'jwtAjaxAction' => $this->jwt->get_client_action_name(),
280 )
281 );
282 }
283 return $params;
284 }
285
286 /**
287 * Injects vendor scripts.
288 *
289 * @param string $plugin_dir_url The plugin dir url.
290 * @param string $webpack_files The files to inject.
291 */
292 private function inject_vendor( $plugin_dir_url, $webpack_files ) {
293 wp_enqueue_script( 'civist_manifest', $plugin_dir_url . $webpack_files->manifest->entry, array(), $webpack_files->manifest->hash, true );
294 wp_enqueue_script( 'civist_vendor', $plugin_dir_url . $webpack_files->vendor->entry, array(), $webpack_files->vendor->hash, true );
295 wp_enqueue_script( 'civist_common', $plugin_dir_url . $webpack_files->common->entry, array(), $webpack_files->common->hash, true );
296 }
297 }
298