PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.1
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.1
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / includes / scripts.php

scripts.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.1, at includes/scripts.php

389 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Scripts
4 *
5 * @package GamiPress\Scripts
6 * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register frontend scripts
14 *
15 * @since 1.0.0
16 * @return void
17 */
18 function gamipress_register_scripts() {
19
20 // Use minified libraries if SCRIPT_DEBUG is turned off
21 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
22
23 // Stylesheets
24 wp_register_style( 'gamipress-css', GAMIPRESS_URL . 'assets/css/gamipress' . $suffix . '.css', array( ), GAMIPRESS_VER, 'all' );
25 wp_register_style( 'gamipress-admin-bar-css', GAMIPRESS_URL . 'assets/css/gamipress-admin-bar' . $suffix . '.css', array( ), GAMIPRESS_VER, 'all' );
26
27 // Scripts
28 wp_register_script( 'gamipress-js', GAMIPRESS_URL . 'assets/js/gamipress' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, true );
29
30 }
31 add_action( 'init', 'gamipress_register_scripts' );
32
33 /**
34 * Enqueue frontend scripts
35 *
36 * @since 1.0.0
37 * @return void
38 */
39 function gamipress_enqueue_scripts( $hook = null ) {
40
41 // Stylesheets
42 wp_enqueue_style( 'gamipress-css' );
43
44 // Scripts
45 $achievement_fields = array();
46
47 if( isset( GamiPress()->shortcodes['gamipress_achievement'] ) && is_array( GamiPress()->shortcodes['gamipress_achievement']->fields ) ) {
48 $achievement_fields = array_keys( GamiPress()->shortcodes['gamipress_achievement']->fields );
49 }
50
51 wp_localize_script( 'gamipress-js', 'gamipress', array(
52 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php', 'relative' ) ),
53 'nonce' => gamipress_get_nonce(),
54 'achievement_fields' => $achievement_fields
55 ) );
56
57 wp_enqueue_script( 'gamipress-js' );
58
59 }
60 add_action( 'wp_enqueue_scripts', 'gamipress_enqueue_scripts' );
61
62 /**
63 * Register admin scripts
64 *
65 * @since 1.0.0
66 * @return void
67 */
68 function gamipress_admin_register_scripts() {
69
70 // Use minified libraries if SCRIPT_DEBUG is turned off
71 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
72
73 // Libraries
74 wp_register_style( 'gamipress-select2-css', GAMIPRESS_URL . 'assets/libs/select2/css/select2' . $suffix . '.css', array( ), GAMIPRESS_VER, 'all' );
75 wp_register_script( 'gamipress-select2-js', GAMIPRESS_URL . 'assets/js/gamipress-select2' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, true );
76 wp_register_script( 'gamipress-fabric-js', GAMIPRESS_URL . 'assets/libs/fabric/fabric.min.js', array( 'jquery' ), GAMIPRESS_VER, true );
77
78 // Stylesheets
79 wp_register_style( 'gamipress-admin-css', GAMIPRESS_URL . 'assets/css/gamipress-admin' . $suffix . '.css', array( ), GAMIPRESS_VER, 'all' );
80
81 // Scripts
82 wp_register_script( 'gamipress-admin-functions-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-functions' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, true );
83 wp_register_script( 'gamipress-admin-js', GAMIPRESS_URL . 'assets/js/gamipress-admin' . $suffix . '.js', array( 'jquery', 'gamipress-admin-functions-js', 'gamipress-select2-js' ), GAMIPRESS_VER, true );
84 wp_register_script( 'gamipress-admin-notices-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-notices' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, false );
85 wp_register_script( 'gamipress-admin-widgets-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-widgets' . $suffix . '.js', array( 'jquery', 'gamipress-admin-functions-js', 'gamipress-select2-js' ), GAMIPRESS_VER, true );
86 wp_register_script( 'gamipress-requirements-ui-js', GAMIPRESS_URL . 'assets/js/gamipress-requirements-ui' . $suffix . '.js', array( 'jquery', 'jquery-ui-sortable', 'gamipress-admin-functions-js', 'gamipress-select2-js' ), GAMIPRESS_VER, true );
87 wp_register_script( 'gamipress-log-extra-data-ui-js', GAMIPRESS_URL . 'assets/js/gamipress-log-extra-data-ui' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, true );
88 wp_register_script( 'gamipress-admin-settings-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-settings' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_VER, true );
89 wp_register_script( 'gamipress-admin-tools-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-tools' . $suffix . '.js', array( 'jquery', 'jquery-ui-dialog', 'gamipress-admin-functions-js', 'gamipress-select2-js' ), GAMIPRESS_VER, true );
90 wp_register_script( 'gamipress-admin-badge-builder-js', GAMIPRESS_URL . 'assets/js/gamipress-admin-badge-builder' . $suffix . '.js', array( 'jquery', 'gamipress-fabric-js' ), GAMIPRESS_VER, true );
91 wp_register_script( 'gamipress-badge-builder-editor-js', GAMIPRESS_URL . 'assets/js/gamipress-badge-builder-editor.min.js', array( 'jquery', 'gamipress-admin-badge-builder-js' ), GAMIPRESS_VER, true );
92
93 }
94 add_action( 'admin_init', 'gamipress_admin_register_scripts' );
95
96 /**
97 * Enqueue admin scripts
98 *
99 * @since 1.0.0
100 * @return void
101 */
102 function gamipress_admin_enqueue_scripts( $hook ) {
103
104 global $post_type;
105
106 // Stylesheets
107 wp_enqueue_style( 'gamipress-admin-css' );
108
109 // Localize admin script
110 wp_localize_script( 'gamipress-admin-js', 'gamipress_admin', array(
111 'nonce' => gamipress_get_admin_nonce(),
112 ) );
113
114 // Localize admin script
115 wp_localize_script( 'gamipress-admin-notices-js', 'gamipress_admin_notices', array(
116 'nonce' => gamipress_get_admin_nonce(),
117 ) );
118
119 // Scripts
120 wp_enqueue_script( 'gamipress-admin-js' );
121 wp_enqueue_script( 'gamipress-admin-notices-js' );
122
123
124 // Enqueue admin functions
125 gamipress_enqueue_admin_functions_script();
126
127 // Requirements ui script
128 if (
129 $post_type === 'points-type'
130 || in_array( $post_type, gamipress_get_achievement_types_slugs() )
131 || in_array( $post_type, gamipress_get_rank_types_slugs() )
132 ) {
133 // Localize requirements ui script
134 wp_localize_script( 'gamipress-requirements-ui-js', 'gamipress_requirements_ui', array(
135 'nonce' => gamipress_get_admin_nonce(),
136 'post_placeholder' => __( 'Select a Post', 'gamipress' ),
137 'unsaved_changes_text' => __( 'Unsaved Changes', 'gamipress' ),
138 'specific_activity_triggers' => gamipress_get_specific_activity_triggers(),
139 'triggers_excluded_from_limit' => gamipress_get_activity_triggers_excluded_from_activity_limit(),
140 'points_triggers' => gamipress_get_points_triggers(),
141 'achievement_type_triggers' => gamipress_get_achievement_type_triggers(),
142 'rank_type_triggers' => gamipress_get_rank_type_triggers(),
143 'post_type_triggers' => gamipress_get_post_type_triggers(),
144 'user_role_triggers' => gamipress_get_user_role_triggers(),
145 ) );
146
147 wp_enqueue_script( 'gamipress-requirements-ui-js' );
148 }
149
150 // Logs scripts
151 if (
152 $hook === 'gamipress_page_gamipress_logs'
153 || $hook === 'admin_page_edit_gamipress_logs'
154 ) {
155 // Localize log extra data ui script
156 wp_localize_script( 'gamipress-log-extra-data-ui-js', 'gamipress_log_extra_data_ui', array(
157 'nonce' => gamipress_get_admin_nonce(),
158 ) );
159
160 wp_enqueue_script( 'gamipress-log-extra-data-ui-js' );
161 }
162
163 // Widgets scripts
164 if( $hook === 'widgets.php' ) {
165 wp_localize_script( 'gamipress-admin-widgets-js', 'gamipress_admin_widgets', array(
166 'nonce' => gamipress_get_admin_nonce(),
167 'id_placeholder' => __( 'Select a Post', 'gamipress' ),
168 'id_multiple_placeholder' => __( 'Select Post(s)', 'gamipress' ),
169 'user_placeholder' => __( 'Select an User', 'gamipress' ),
170 'post_type_placeholder' => __( 'Default: All', 'gamipress' ),
171 'rank_placeholder' => __( 'Select a Rank', 'gamipress' ),
172 ) );
173
174 wp_enqueue_script( 'gamipress-admin-widgets-js' );
175 }
176
177 // Settings page
178 if( $hook === 'gamipress_page_gamipress_settings' ) {
179 wp_enqueue_script( 'gamipress-admin-settings-js' );
180 }
181
182 // Tools page
183 if( $hook === 'gamipress_page_gamipress_tools' ) {
184
185 $user = get_userdata( get_current_user_id() );
186
187 wp_localize_script( 'gamipress-admin-tools-js', 'gamipress_admin_tools', array(
188 'nonce' => gamipress_get_admin_nonce(),
189 // Notices
190 'recount_activity_notice' => __( 'Please be patient while this process is running. This can take a while, up to some minutes. Do not navigate away from this page until this script is done. You will be notified via this page when the recount process is completed.', 'gamipress' ),
191 // User data (used as sample on CSV templates)
192 'user_id' => $user->ID,
193 'user_name' => $user->user_login,
194 'user_email' => $user->user_email,
195 ) );
196
197 wp_enqueue_script( 'gamipress-admin-tools-js' );
198
199 // Enqueue WordPress jQuery UI Dialog style
200 wp_enqueue_style ( 'wp-jquery-ui-dialog' );
201
202 }
203
204 // Badge Builder
205 if( $hook === 'gamipress_page_gamipress_badge_builder' ) {
206
207 $icons = gamipress_badge_builder_get_icons();
208 $shapes_ids = array_keys( $icons['shapes']['icons'] );
209 $icons_ids = array();
210 foreach( $icons as $group_id => $group ) {
211 if( in_array( $group_id, array( 'shapes', 'editor', 'logos' ) ) ) continue;
212
213 $icons_ids = array_merge( $icons_ids, array_keys( $icons[$group_id]['icons'] ) );
214 }
215
216 // Scripts
217 wp_localize_script( 'gamipress-admin-badge-builder-js', 'gamipress_admin_badge_builder', array(
218 'nonce' => gamipress_get_admin_nonce(),
219 'color_palettes' => gamipress_badge_builder_get_color_palettes(),
220 'insert_text_here' => __( 'Insert text here', 'gamipress' ),
221 'show_text' => __( 'Show', 'gamipress' ),
222 'hide_text' => __( 'Hide', 'gamipress' ),
223 'shapes' => $shapes_ids,
224 'icons' => $icons_ids,
225 ) );
226
227 wp_enqueue_script( 'gamipress-fabric-js' );
228 wp_enqueue_script( 'gamipress-admin-badge-builder-js' );
229 wp_enqueue_script( 'gamipress-badge-builder-editor-js' );
230
231 }
232
233 }
234 add_action( 'admin_enqueue_scripts', 'gamipress_admin_enqueue_scripts' );
235
236 /**
237 * Register and enqueue admin bar scripts
238 *
239 * @since 1.5.1
240 * @return void
241 */
242 function gamipress_enqueue_admin_bar_scripts() {
243
244 wp_enqueue_style( 'gamipress-admin-bar-css' );
245
246 }
247 add_action( 'admin_bar_init', 'gamipress_enqueue_admin_bar_scripts' );
248
249 /**
250 * Enqueue Gutenberg block assets for backend editor
251 *
252 * @since 1.6.0
253 */
254 function gamipress_blocks_editor_assets() {
255
256 // Scripts
257 wp_enqueue_script( 'gamipress-blocks-js', GAMIPRESS_URL . 'assets/js/gamipress-blocks.js', array( 'wp-blocks', 'wp-i18n', 'wp-element' ), GAMIPRESS_VER, true );
258
259 // Styles
260 wp_enqueue_style( 'gamipress-blocks-editor-css', GAMIPRESS_URL . 'assets/css/gamipress-blocks-editor.css', array( 'wp-edit-blocks' ), GAMIPRESS_VER );
261 }
262 add_action( 'enqueue_block_editor_assets', 'gamipress_blocks_editor_assets' );
263
264 /**
265 * Localize block assets.
266 *
267 * @since 1.6.0
268 */
269 function gamipress_localize_blocks_editor_assets() {
270
271 // Setup shortcode to be converted as gutenberg block
272 $shortcodes = array();
273
274 foreach( GamiPress()->shortcodes as $shortcode ) {
275
276 $shortcodes[$shortcode->slug] = array(
277 'name' => $shortcode->name,
278 'slug' => $shortcode->slug,
279 'icon' => $shortcode->icon,
280 'fields' => gamipress_get_block_fields( $shortcode ),
281 'tabs' => $shortcode->tabs,
282 'attributes' => gamipress_get_block_attributes( $shortcode ),
283 );
284
285 }
286
287 // Setup an array of post type labels to use on post selector field
288 $post_types = get_post_types( array(), 'objects' );
289 $post_type_labels = array();
290
291 foreach( $post_types as $key => $obj ) {
292 $post_type_labels[$key] = $obj->labels->singular_name;
293 }
294
295 // Localize gamipress-blocks-js script
296 wp_localize_script( 'gamipress-blocks-js', 'gamipress_blocks', array(
297 'shortcodes' => $shortcodes,
298 'icons' => gamipress_get_block_icons(),
299 'post_type_labels' => $post_type_labels,
300 ) );
301
302 }
303 add_action( 'enqueue_block_editor_assets', 'gamipress_localize_blocks_editor_assets', 11 );
304
305 /**
306 * Enqueue block content assets.
307 *
308 * @since 1.6.0
309 */
310 function gamipress_enqueue_block_assets() {
311 if ( is_admin() ) {
312 gamipress_enqueue_scripts();
313 }
314 }
315 add_action( 'enqueue_block_assets', 'gamipress_enqueue_block_assets', 11 );
316
317 /**
318 * Enqueue the admin functions script with all required components
319 *
320 * @since 1.7.4.1
321 * @return void
322 */
323 function gamipress_enqueue_admin_functions_script() {
324
325 // Enqueue Select2 library
326 wp_enqueue_script( 'gamipress-select2-js' );
327 wp_enqueue_style( 'gamipress-select2-css' );
328
329 // Setup an array of post type labels to use on post selector field
330 $post_types = get_post_types( array(), 'objects' );
331 $post_type_labels = array();
332
333 foreach( $post_types as $key => $obj ) {
334 $post_type_labels[$key] = $obj->labels->singular_name;
335 }
336
337 // Localize admin functions script
338 wp_localize_script( 'gamipress-admin-functions-js', 'gamipress_admin_functions', array(
339 'nonce' => gamipress_get_admin_nonce(),
340 'post_type_labels' => $post_type_labels,
341 'reserved_terms' => gamipress_get_reserved_terms(),
342 // Selector placeholders
343 'selector_placeholder' => __( 'Select an option', 'gamipress' ),
344 'post_selector_placeholder' => __( 'Select a post', 'gamipress' ),
345 'user_selector_placeholder' => __( 'Select a user', 'gamipress' ),
346 // Slug error messages
347 'slug_error_special_char' => __( 'Slug can\'t contain special characters. Only alphanumeric characters are allowed.', 'gamipress' ),
348 'slug_error_max_length' => __( 'Slug supports a maximum of 20 characters.', 'gamipress' ),
349 'slug_error_post_type' => __( 'The %s post type already uses this slug.', 'gamipress' ),
350 'slug_error_reserved_term' => __( 'Slug can\'t match any <a href="https://codex.wordpress.org/Reserved_Terms">WordPress reserved term</a>.', 'gamipress' ),
351
352 ) );
353
354 wp_enqueue_script( 'gamipress-admin-functions-js' );
355
356 }
357
358 /**
359 * Setup a global nonce for all frontend scripts
360 *
361 * @since 1.7.9
362 *
363 * @return string
364 */
365 function gamipress_get_nonce() {
366
367 if( ! defined( 'GAMIPRESS_NONCE' ) )
368 define( 'GAMIPRESS_NONCE', wp_create_nonce( 'gamipress' ) );
369
370 return GAMIPRESS_NONCE;
371
372 }
373
374 /**
375 * Setup a global nonce for all admin scripts
376 *
377 * @since 1.7.9
378 *
379 * @return string
380 */
381 function gamipress_get_admin_nonce() {
382
383 if( ! defined( 'GAMIPRESS_ADMIN_NONCE' ) )
384 define( 'GAMIPRESS_ADMIN_NONCE', wp_create_nonce( 'gamipress_admin' ) );
385
386 return GAMIPRESS_ADMIN_NONCE;
387
388 }
389