PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 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 All 47 releases
← All changes | includes/admin.php +802 -0 7.9.9.2 → 8.0.5 View file →
@@ -1,0 +1,802 @@
1 +<?php
2 +/**
3 + * Admin
4 + *
5 + * @package GamiPress\Admin
6 + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +// Admin includes
13 +require_once GAMIPRESS_DIR . 'includes/admin/contextual-help.php';
14 +require_once GAMIPRESS_DIR . 'includes/admin/dashboard.php';
15 +require_once GAMIPRESS_DIR . 'includes/admin/debug.php';
16 +require_once GAMIPRESS_DIR . 'includes/admin/meta-boxes.php';
17 +require_once GAMIPRESS_DIR . 'includes/admin/notices.php';
18 +require_once GAMIPRESS_DIR . 'includes/admin/plugins.php';
19 +require_once GAMIPRESS_DIR . 'includes/admin/achievements.php';
20 +require_once GAMIPRESS_DIR . 'includes/admin/ranks.php';
21 +require_once GAMIPRESS_DIR . 'includes/admin/requirements.php';
22 +require_once GAMIPRESS_DIR . 'includes/admin/users.php';
23 +require_once GAMIPRESS_DIR . 'includes/admin/upgrades.php';
24 +
25 +// Admin pages
26 +require_once GAMIPRESS_DIR . 'includes/admin/pages/dashboard.php';
27 +require_once GAMIPRESS_DIR . 'includes/admin/pages/badge-builder.php';
28 +require_once GAMIPRESS_DIR . 'includes/admin/pages/tools.php';
29 +require_once GAMIPRESS_DIR . 'includes/admin/pages/settings.php';
30 +require_once GAMIPRESS_DIR . 'includes/admin/pages/licenses.php';
31 +require_once GAMIPRESS_DIR . 'includes/admin/pages/add-ons.php';
32 +/**
33 + * Add custom GamiPress body classes
34 + *
35 + * @since 1.3.9.5
36 + *
37 + * @param string $admin_body_classes
38 + *
39 + * @return string
40 + */
41 +function gamipress_admin_body_class( $admin_body_classes ) {
42 +
43 + global $post_type;
44 +
45 + // Add an extra class to meet that current post type is a gamipress post type
46 + if(
47 + in_array( $post_type, array( 'points-type', 'achievement-type', 'rank-type' ) )
48 + || in_array( $post_type, gamipress_get_achievement_types_slugs() )
49 + || in_array( $post_type, gamipress_get_rank_types_slugs() )
50 + ) {
51 + $admin_body_classes .= ' gamipress-post-type ';
52 + }
53 +
54 + return $admin_body_classes;
55 +
56 +}
57 +add_filter( 'admin_body_class', 'gamipress_admin_body_class' );
58 +
59 +/**
60 + * Add GamiPress menus
61 + *
62 + * @since 1.0.0
63 + * @updated 1.4.0 Added multisite support
64 + */
65 +function gamipress_admin_menu() {
66 +
67 + // Bail if GamiPress is active network wide and we are not in main site
68 + if( gamipress_is_network_wide_active() && ! is_main_site() ) {
69 + return;
70 + }
71 +
72 + // Set minimum role setting for menus
73 + $minimum_role = gamipress_get_manager_capability();
74 +
75 + // Achievement types
76 + $achievement_types = gamipress_get_achievement_types();
77 +
78 + // Achievements menu
79 + if( ! empty( $achievement_types ) ) {
80 + add_menu_page( __( 'Achievements', 'gamipress' ), __( 'Achievements', 'gamipress' ), $minimum_role, 'gamipress_achievements', 'gamipress_achievements', 'dashicons-awards', 53 );
81 + }
82 +
83 + // Rank types
84 + $rank_types = gamipress_get_rank_types();
85 +
86 + // Achievements menu
87 + if( ! empty( $rank_types ) ) {
88 + add_menu_page( __( 'Ranks', 'gamipress' ), __( 'Ranks', 'gamipress' ), $minimum_role, 'gamipress_ranks', 'gamipress_ranks', 'dashicons-rank', 54 );
89 + }
90 +
91 + // GamiPress menu
92 + add_menu_page( 'GamiPress', 'GamiPress', $minimum_role, 'gamipress', '', 'dashicons-gamipress', 55 );
93 + add_submenu_page( 'gamipress', __( 'Dashboard', 'gamipress' ), __( 'Dashboard', 'gamipress' ), $minimum_role, 'gamipress', 'gamipress_dashboard_page' );
94 +
95 +}
96 +add_action( 'admin_menu', 'gamipress_admin_menu' );
97 +
98 +/**
99 + * Moves the Dashboard submenu to the first menu
100 + * For some reason, WordPress assigns the first submenu item the post types registered under this menu
101 + *
102 + * @since 1.0.0
103 + *
104 + * @param string $parent_file The parent file.
105 + *
106 + * @return string
107 + */
108 +function gamipress_admin_menu_fix( $parent_file ) {
109 +
110 + global $submenu;
111 +
112 +
113 + if( isset( $submenu['gamipress'] ) && is_array( $submenu['gamipress'] ) ) {
114 +
115 + // Loop all the GamiPress submenus
116 + foreach( $submenu['gamipress'] as $i => $menu ) {
117 +
118 + // Check for the "Dashboard" menu to move it to first position
119 + if( is_array( $menu ) && isset( $menu[3] ) && $menu[3] === __( 'Dashboard', 'gamipress' ) ) {
120 + unset( $submenu['gamipress'][$i] );
121 + array_unshift( $submenu['gamipress'], $menu );
122 + break;
123 + }
124 +
125 + }
126 +
127 + if( isset( $submenu['gamipress'][7] )
128 + && isset( $submenu['gamipress'][8] )
129 + && isset( $submenu['gamipress'][9] ) ) {
130 +
131 + // Reorder licenses, tools & settings
132 + $licenses = $submenu['gamipress'][7];
133 + $tools = $submenu['gamipress'][8];
134 + $settings = $submenu['gamipress'][9];
135 +
136 + $submenu['gamipress'][7] = $tools;
137 + $submenu['gamipress'][8] = $settings;
138 + $submenu['gamipress'][9] = $licenses;
139 +
140 + }
141 + }
142 +
143 + return $parent_file;
144 +
145 +}
146 +add_filter( 'parent_file', 'gamipress_admin_menu_fix' );
147 +
148 +/**
149 + * Add GamiPress submenus
150 + *
151 + * @since 1.0.0
152 + */
153 +function gamipress_admin_submenu() {
154 +
155 + // Set minimum role setting for menus
156 + $minimum_role = gamipress_get_manager_capability();
157 +
158 + // Assets sub menu
159 + add_submenu_page( 'gamipress', __( 'Badge Builder', 'gamipress' ), __( 'Badge Builder', 'gamipress' ), $minimum_role, 'gamipress_badge_builder', 'gamipress_badge_builder_page' );
160 +
161 +
162 +}
163 +add_action( 'admin_menu', 'gamipress_admin_submenu', 12 );
164 +
165 +function gamipress_admin_addons_submenu() {
166 + // Set minimum role setting for menus
167 + $minimum_role = gamipress_get_manager_capability();
168 +
169 + // Add-ons sub menu
170 + add_submenu_page( 'gamipress', __( 'Add-ons', 'gamipress' ), __( 'Add-ons', 'gamipress' ), $minimum_role, 'gamipress_add_ons', 'gamipress_add_ons_page' );
171 +}
172 +add_action( 'admin_menu', 'gamipress_admin_addons_submenu', 15 );
173 +
174 +/**
175 + * Helper function to get the try of the day
176 + *
177 + * @since 1.0.0
178 + *
179 + * @return array|false
180 + */
181 +function gamipress_get_try_option() {
182 +
183 + $options = array();
184 +
185 + if( ! class_exists( 'AutomatorWP' ) ) {
186 + $options[] = array(
187 + 'label' => __( 'Try AutomatorWP!', 'gamipress' ),
188 + 'slug' => 'automatorwp'
189 + );
190 + }
191 +
192 + if( ! class_exists( 'ShortLinksPro' ) ) {
193 + $options[] = array(
194 + 'label' => __( 'Try ShortLinks Pro!', 'gamipress' ),
195 + 'slug' => 'shortlinkspro'
196 + );
197 + }
198 +
199 + if( ! class_exists( 'BBForms' ) ) {
200 + $options[] = array(
201 + 'label' => __( 'Try BBForms!', 'gamipress' ),
202 + 'slug' => 'bbforms'
203 + );
204 + }
205 +
206 + $count = count( $options );
207 +
208 + // Bail if no options found
209 + if( $count === 0 ) return false;
210 +
211 + $day = absint( gmdate( 'd' ) );
212 + $index = $day % $count;
213 +
214 + if( ! isset( $options[ $index ] ) ) {
215 + $index = 0;
216 + }
217 +
218 + return $options[ $index ];
219 +
220 +}
221 +
222 +/**
223 + * Add Try submenu
224 + *
225 + * @since 1.0.0
226 + */
227 +function gamipress_try_admin_submenu() {
228 +
229 + $option = gamipress_get_try_option();
230 +
231 + if( ! $option ) return;
232 +
233 + // Set minimum role for menu
234 + $minimum_role = gamipress_get_manager_capability();
235 +
236 + $badge = '<span class="gamipress-admin-menu-badge">' . __( 'New', 'gamipress' ) . '</span>';
237 +
238 + add_submenu_page( 'gamipress', $option['label'], $option['label'] . $badge, $minimum_role, 'https://wordpress.org/plugins/' . $option['slug'] . '/', null );
239 +}
240 +add_action( 'admin_menu', 'gamipress_try_admin_submenu', 9999 );
241 +
242 +/**
243 + * Helper funtion to meet if should show the admin bar menu
244 + *
245 + * @since 2.0.3
246 + */
247 +function gamipress_show_admin_bar_menu() {
248 +
249 + // Bail if GamiPress is active network wide and we are not in main site
250 + if( gamipress_is_network_wide_active() && ! is_main_site() ) {
251 + return false;
252 + }
253 +
254 + // Bail if current user can't manage GamiPress
255 + if ( ! current_user_can( gamipress_get_manager_capability() ) ) {
256 + return false;
257 + }
258 +
259 + // Bail if admin bar menu disabled
260 + if( (bool) gamipress_get_option( 'disable_admin_bar_menu', false ) ) {
261 + return false;
262 + }
263 +
264 + return true;
265 +
266 +}
267 +
268 +/**
269 + * Add GamiPress admin bar menu
270 + *
271 + * @since 1.5.1
272 + *
273 + * @param WP_Admin_Bar $wp_admin_bar
274 + */
275 +function gamipress_admin_bar_menu( $wp_admin_bar ) {
276 +
277 + // Bail if admin bar menu disabled
278 + if( ! gamipress_show_admin_bar_menu() ) {
279 + return;
280 + }
281 +
282 + // GamiPress
283 + $wp_admin_bar->add_node( array(
284 + 'id' => 'gamipress',
285 + 'title' => '<span class="ab-icon"></span>' . 'GamiPress',
286 + 'href' => admin_url( 'admin.php?page=gamipress' ),
287 + 'meta' => array( 'class' => 'gamipress' ),
288 + ) );
289 +
290 + // Dashboard Group
291 + $wp_admin_bar->add_group( array(
292 + 'id' => 'gamipress-dashboard-group',
293 + 'parent' => 'gamipress',
294 + ) );
295 +
296 + // Dashboard
297 + $wp_admin_bar->add_node( array(
298 + 'id' => 'gamipress-dashboard',
299 + 'title' => __( 'Dashboard', 'gamipress' ),
300 + 'parent' => 'gamipress-dashboard-group',
301 + 'href' => admin_url( 'admin.php?page=gamipress' )
302 + ) );
303 +
304 + // Check registered points types
305 + $points_types = gamipress_get_points_types();
306 +
307 + // - Points Types
308 + $wp_admin_bar->add_node( array(
309 + 'id' => 'points-types',
310 + 'title' => __( 'Points Types', 'gamipress' ),
311 + 'parent' => ( ! empty( $points_types ) ? 'points-group' : 'gamipress' ),
312 + 'href' => admin_url( 'edit.php?post_type=points-type' )
313 + ) );
314 +
315 + if( ! empty( $points_types ) ) {
316 +
317 + // Points Group
318 + $wp_admin_bar->add_group( array(
319 + 'id' => 'points-group',
320 + 'parent' => 'gamipress',
321 + ) );
322 +
323 + foreach( $points_types as $points_type => $data ) {
324 +
325 + // - - Achievements
326 + $wp_admin_bar->add_node( array(
327 + 'id' => 'points-' . $points_type,
328 + 'title' => $data['plural_name'],
329 + 'parent' => 'points-types',
330 + 'href' => get_edit_post_link( $data['ID'] )
331 + ) );
332 +
333 + }
334 +
335 + }
336 +
337 + // Check registered achievement types
338 + $achievement_types = gamipress_get_achievement_types();
339 +
340 + // - Achievement Types
341 + $wp_admin_bar->add_node( array(
342 + 'id' => 'achievement-types',
343 + 'title' => __( 'Achievement Types', 'gamipress' ),
344 + 'parent' => ( ! empty( $achievement_types ) ? 'achievements-group' : 'gamipress' ),
345 + 'href' => admin_url( 'edit.php?post_type=achievement-type' )
346 + ) );
347 +
348 + if( ! empty( $achievement_types ) ) {
349 +
350 + // Achievements Group
351 + $wp_admin_bar->add_group( array(
352 + 'id' => 'achievements-group',
353 + 'parent' => 'gamipress',
354 + ) );
355 +
356 + foreach( $achievement_types as $achievement_type => $data ) {
357 +
358 + // - - Achievements
359 + $wp_admin_bar->add_node( array(
360 + 'id' => 'achievement-' . $achievement_type,
361 + 'title' => $data['plural_name'],
362 + 'parent' => 'achievement-types',
363 + 'href' => admin_url( 'edit.php?post_type=' . $achievement_type )
364 + ) );
365 +
366 + }
367 +
368 + }
369 +
370 + // Check registered rank types
371 + $rank_types = gamipress_get_rank_types();
372 +
373 + // - Rank Types
374 + $wp_admin_bar->add_node( array(
375 + 'id' => 'rank-types',
376 + 'title' => __( 'Rank Types', 'gamipress' ),
377 + 'parent' => ( ! empty( $rank_types ) ? 'ranks-group' : 'gamipress' ),
378 + 'href' => admin_url( 'edit.php?post_type=rank-type' )
379 + ) );
380 +
381 + if( ! empty( $rank_types ) ) {
382 +
383 + // Achievements Group
384 + $wp_admin_bar->add_group( array(
385 + 'id' => 'ranks-group',
386 + 'parent' => 'gamipress',
387 + ) );
388 +
389 + foreach( $rank_types as $rank_type => $data ) {
390 +
391 + // - - Achievements
392 + $wp_admin_bar->add_node( array(
393 + 'id' => 'rank-' . $rank_type,
394 + 'title' => $data['plural_name'],
395 + 'parent' => 'rank-types',
396 + 'href' => admin_url( 'edit.php?post_type=' . $rank_type )
397 + ) );
398 +
399 + }
400 +
401 + }
402 +
403 +}
404 +add_action( 'admin_bar_menu', 'gamipress_admin_bar_menu', 100 );
405 +
406 +/**
407 + * Add GamiPress admin bar custom tables menu
408 + *
409 + * @since 1.5.4
410 + */
411 +function gamipress_admin_bar_custom_tables_menu( $wp_admin_bar ) {
412 +
413 + // Bail if admin bar menu disabled
414 + if( ! gamipress_show_admin_bar_menu() ) {
415 + return;
416 + }
417 +
418 + // User Earnings
419 + $wp_admin_bar->add_node( array(
420 + 'id' => 'gamipress-user-earnings',
421 + 'title' => __( 'User Earnings', 'gamipress' ),
422 + 'parent' => 'gamipress',
423 + 'href' => admin_url( 'admin.php?page=gamipress_user_earnings' )
424 + ) );
425 +
426 + // Logs
427 + $wp_admin_bar->add_node( array(
428 + 'id' => 'gamipress-logs',
429 + 'title' => __( 'Logs', 'gamipress' ),
430 + 'parent' => 'gamipress',
431 + 'href' => admin_url( 'admin.php?page=gamipress_logs' )
432 + ) );
433 +
434 +}
435 +add_action( 'admin_bar_menu', 'gamipress_admin_bar_custom_tables_menu', 150 );
436 +
437 +/**
438 + * Add GamiPress admin bar menu
439 + *
440 + * @since 1.5.1
441 + *
442 + * @param object $wp_admin_bar The WordPress toolbar object
443 + */
444 +function gamipress_admin_bar_submenu( $wp_admin_bar ) {
445 +
446 + // Bail if admin bar menu disabled
447 + if( ! gamipress_show_admin_bar_menu() ) {
448 + return;
449 + }
450 +
451 + // Badge Builder
452 + $wp_admin_bar->add_node( array(
453 + 'id' => 'gamipress-badge-builder',
454 + 'title' => __( 'Badge Builder', 'gamipress' ),
455 + 'parent' => 'gamipress',
456 + 'href' => admin_url( 'admin.php?page=gamipress_badge_builder' )
457 + ) );
458 +
459 + // Tools
460 + $wp_admin_bar->add_node( array(
461 + 'id' => 'gamipress-tools',
462 + 'title' => __( 'Tools', 'gamipress' ),
463 + 'parent' => 'gamipress',
464 + 'href' => admin_url( 'admin.php?page=gamipress_tools' )
465 + ) );
466 +
467 + // Settings
468 + $wp_admin_bar->add_node( array(
469 + 'id' => 'gamipress-settings',
470 + 'title' => __( 'Settings', 'gamipress' ),
471 + 'parent' => 'gamipress',
472 + 'href' => admin_url( 'admin.php?page=gamipress_settings' )
473 + ) );
474 +
475 + // Licenses
476 + $wp_admin_bar->add_node( array(
477 + 'id' => 'gamipress-licenses',
478 + 'title' => __( 'Licenses', 'gamipress' ),
479 + 'parent' => 'gamipress',
480 + 'href' => admin_url( 'admin.php?page=gamipress_licenses' )
481 + ) );
482 +
483 + // Add-ons
484 + $wp_admin_bar->add_node( array(
485 + 'id' => 'gamipress-add-ons',
486 + 'title' => __( 'Add-ons', 'gamipress' ),
487 + 'parent' => 'gamipress',
488 + 'href' => admin_url( 'admin.php?page=gamipress_add_ons' )
489 + ) );
490 +
491 +}
492 +add_action( 'admin_bar_menu', 'gamipress_admin_bar_submenu', 999 );
493 +
494 +/**
495 + * Add Try admin bar submenu
496 + *
497 + * @since 1.0.0
498 + *
499 + * @param object $wp_admin_bar The WordPress toolbar object
500 + */
501 +function gamipress_try_admin_bar_submenu( $wp_admin_bar ) {
502 +
503 + // Bail if current user can't manage
504 + if ( ! current_user_can( gamipress_get_manager_capability() ) ) {
505 + return;
506 + }
507 +
508 + // Bail if admin bar menu disabled
509 + if( (bool) gamipress_get_option( 'disable_admin_bar_menu', false ) ) {
510 + return;
511 + }
512 +
513 + $option = gamipress_get_try_option();
514 +
515 + if( ! $option ) return;
516 +
517 + $badge = '<span class="gamipress-admin-menu-badge">' . __( 'New', 'gamipress' ) . '</span>';
518 +
519 + // Try AutomatorWP
520 + $wp_admin_bar->add_node( array(
521 + 'id' => 'gamipress-try',
522 + 'title' => $option['label'] . $badge,
523 + 'parent' => 'gamipress',
524 + 'href' => 'https://wordpress.org/plugins/' . $option['slug'] . '/'
525 + ) );
526 +
527 +}
528 +add_action( 'admin_bar_menu', 'gamipress_try_admin_bar_submenu', 999 );
529 +
530 +/**
531 + * Register our custom columns
532 + *
533 + * @since 1.0.6
534 + * @updated 1.3.9.5 Added the thumbnail column
535 + *
536 + * @param $posts_columns
537 + * @param $post_type
538 + *
539 + * @return mixed
540 + */
541 +function gamipress_posts_columns( $posts_columns, $post_type ) {
542 +
543 + if( ! in_array( $post_type, array( 'points-type', 'achievement-type', 'rank-type' ) ) ) {
544 + return $posts_columns;
545 + }
546 +
547 + // Switch Title to Singular Name
548 + $posts_columns['title'] = __( 'Singular Name', 'gamipress' );
549 +
550 + // Prepend the thumbnail column
551 + $chunks = array_chunk( $posts_columns, 1, true );
552 + $chunks[0]['thumbnail'] = __( 'Image', 'gamipress' );
553 +
554 + $posts_columns = call_user_func_array( 'array_merge', $chunks );
555 +
556 + // Try to place our column before date column
557 + $pos = array_search( 'date', array_keys( $posts_columns ) );
558 +
559 + if ( ! is_int( $pos ) ) {
560 + $pos = 1;
561 + }
562 +
563 + // Place our column in our desired position
564 + $chunks = array_chunk( $posts_columns, $pos, true );
565 + $chunks[0]['plural_name'] = __( 'Plural Name', 'gamipress' );
566 + $chunks[0]['post_name'] = __( 'Slug', 'gamipress' );
567 +
568 + if( $post_type === 'achievement-type' ) {
569 + $chunks[0]['posts_count'] = __( 'Achievements', 'gamipress' );
570 + } else if( $post_type === 'rank-type' ) {
571 + $chunks[0]['posts_count'] = __( 'Ranks', 'gamipress' );
572 + }
573 +
574 + return call_user_func_array( 'array_merge', $chunks );
575 +}
576 +add_filter( 'manage_posts_columns', 'gamipress_posts_columns', 10, 2 );
577 +
578 +/**
579 + * Output for our custom columns
580 + *
581 + * @since 1.0.6
582 + * @updated 1.3.9.5 Added the thumbnail column output
583 + *
584 + * @param $column_name
585 + * @param $post_id
586 + */
587 +function gamipress_posts_custom_columns( $column_name, $post_id ) {
588 +
589 + global $wpdb;
590 +
591 + if( ! in_array( gamipress_get_post_type( $post_id ), array( 'points-type', 'achievement-type', 'rank-type' ) ) ) {
592 + return;
593 + }
594 +
595 + switch( $column_name ) {
596 + case 'thumbnail':
597 + $can_edit_post = current_user_can( 'edit_post', $post_id );
598 +
599 + if( $can_edit_post ) {
600 + printf(
601 + '<a href="%s" aria-label="%s">%s</a>',
602 + get_edit_post_link( $post_id ),
603 + /* translators: %s: post title */
604 + esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), get_post_field( 'post_title', $post_id ) ) ),
605 + gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) )
606 + );
607 + } else {
608 + echo gamipress_get_the_post_thumbnail( $post_id, array( 32, 32 ) );
609 + }
610 +
611 + break;
612 + case 'plural_name':
613 + echo esc_html( gamipress_get_post_meta( $post_id, '_gamipress_plural_name' ) );
614 + break;
615 + case 'post_name':
616 + echo esc_html( gamipress_get_post_field( 'post_name', $post_id ) );
617 + break;
618 + case 'posts_count':
619 + $posts = GamiPress()->db->posts;
620 + $post_type = gamipress_get_post_field( 'post_name', $post_id );
621 +
622 + $count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$posts} WHERE post_type = %s", $post_type ) ) );
623 + $url = admin_url( 'edit.php?post_type=' . $post_type );
624 +
625 + echo '<a href="' . esc_attr( $url ) . '">' . esc_html( $count ) . '</a>';
626 + break;
627 + }
628 +}
629 +add_action( 'manage_posts_custom_column', 'gamipress_posts_custom_columns', 10, 2 );
630 +
631 +/**
632 + * On delete our post, remove relationships
633 + *
634 + * @since 1.1.5
635 + *
636 + * @param integer $post_id
637 + */
638 +function gamipress_on_delete_post( $post_id ) {
639 + global $wpdb;
640 +
641 + $post_type = gamipress_get_post_type( $post_id );
642 +
643 + $is_achievement = gamipress_is_achievement( $post_id );
644 + $is_rank = gamipress_is_rank( $post_id );
645 +
646 + $delete_user_earnings = ( in_array( $post_type, gamipress_get_requirement_types_slugs() ) );
647 +
648 + if( $post_type === 'achievement-type' || $post_type === 'rank-type' ) {
649 + // Remove all achievements of this achievement type or if is a rank type, remove all ranks of this rank type
650 +
651 + $dependent_type = get_post_field( 'post_name', $post_id );
652 +
653 + $posts = GamiPress()->db->posts;
654 +
655 + $dependents = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID FROM {$posts} AS p WHERE p.post_type = %s", $dependent_type ) );
656 +
657 + foreach( $dependents as $dependent ) {
658 + // Remove the achievement or rank of this type
659 + wp_delete_post( $dependent->ID );
660 + }
661 + } else if( $post_type === 'points-type' ) {
662 +
663 + // Get assigned points awards
664 + $points_awards = gamipress_get_points_type_points_awards( $post_id, 'any' );
665 +
666 + if( $points_awards ) {
667 +
668 + foreach( $points_awards as $points_award ) {
669 + // Remove the points award
670 + wp_delete_post( $points_award->ID );
671 + }
672 +
673 + }
674 +
675 + // Get assigned points deducts
676 + $points_deducts = gamipress_get_points_type_points_deducts( $post_id, 'any' );
677 +
678 + if( $points_deducts ) {
679 +
680 + foreach( $points_deducts as $points_deduct ) {
681 + // Remove the points deduct
682 + wp_delete_post( $points_deduct->ID );
683 + }
684 +
685 + }
686 +
687 + } else if( $is_achievement || $is_rank ) {
688 + // Remove steps/rank requirements assigned
689 +
690 + // Force to delete user earnings
691 + $delete_user_earnings = true;
692 +
693 + if( $is_rank ) {
694 + $requirement_type = 'rank-requirement';
695 + } else if ( $is_achievement ) {
696 + $requirement_type = 'step';
697 + }
698 +
699 + if( ! isset( $requirement_type ) ) {
700 + return;
701 + }
702 +
703 + // Get assigned requirements
704 + $assigned_requirements = gamipress_get_assigned_requirements( $post_id, $requirement_type, 'any' );
705 +
706 + if( $assigned_requirements ) {
707 +
708 + foreach( $assigned_requirements as $requirement ) {
709 + // Remove the requirement
710 + wp_delete_post( $requirement->ID );
711 + }
712 +
713 + }
714 + }
715 +
716 + // Delete assigned user earnings
717 + if( $delete_user_earnings ) {
718 +
719 + $user_earnings = GamiPress()->db->user_earnings;
720 + $user_earnings_meta = GamiPress()->db->user_earnings_meta;
721 +
722 + // Delete all user's earnings
723 + $wpdb->query( "DELETE ue FROM {$user_earnings} AS ue WHERE ue.post_id = {$post_id}" );
724 + // Delete orphaned user earnings metas
725 + $wpdb->query( "DELETE uem FROM {$user_earnings_meta} uem LEFT JOIN {$user_earnings} ue ON ue.user_earning_id = uem.user_earning_id WHERE ue.user_earning_id IS NULL" );
726 + }
727 +
728 +}
729 +add_action( 'delete_post', 'gamipress_on_delete_post' );
730 +
731 +/**
732 + * Processes all GamiPress actions sent via POST and GET by looking for the 'gamipress-action' request and running do_action() to call the function
733 + *
734 + * @since 1.1.5
735 + */
736 +function gamipress_process_actions() {
737 +
738 + // $_REQUEST
739 + if ( isset( $_REQUEST['gamipress-action'] ) ) {
740 + do_action( 'gamipress_action_request_' . $_REQUEST['gamipress-action'], $_REQUEST );
741 + }
742 +
743 + // $_POST
744 + if ( isset( $_POST['gamipress-action'] ) ) {
745 + do_action( 'gamipress_action_post_' . $_POST['gamipress-action'], $_POST );
746 + }
747 +
748 + // $_GET
749 + if ( isset( $_GET['gamipress-action'] ) ) {
750 + do_action( 'gamipress_action_get_' . $_GET['gamipress-action'], $_GET );
751 + }
752 +
753 +}
754 +add_action( 'admin_init', 'gamipress_process_actions' );
755 +
756 +/**
757 + * Add custom footer text to the admin dashboard
758 + *
759 + * @since 1.3.8.1
760 + *
761 + * @param string $footer_text The existing footer text
762 + *
763 + * @return string
764 + */
765 +function gamipress_admin_footer_text( $footer_text ) {
766 +
767 + global $typenow;
768 +
769 + if (
770 + $typenow === 'points-type'
771 + || $typenow === 'achievement-type'
772 + || $typenow === 'rank-type'
773 + || in_array( $typenow, gamipress_get_achievement_types_slugs() )
774 + || in_array( $typenow, gamipress_get_rank_types_slugs() )
775 + || ( isset( $_GET['page'] ) && (
776 + $_GET['page'] === 'gamipress_settings'
777 + || $_GET['page'] === 'gamipress_logs'
778 + || $_GET['page'] === 'edit_gamipress_logs'
779 + || $_GET['page'] === 'gamipress_add_ons'
780 + || $_GET['page'] === 'gamipress_badge_builder'
781 + || $_GET['page'] === 'gamipress_tools'
782 + || $_GET['page'] === 'gamipress'
783 + )
784 + )
785 + ) {
786 +
787 + $gamipress_footer_text = sprintf( __( 'Thank you for using <a href="%1$s" target="_blank">GamiPress</a>! Please leave us a <a href="%2$s" target="_blank">%3$s</a> rating on WordPress.org', 'gamipress' ),
788 + 'https://gamipress.com',
789 + 'https://wordpress.org/support/plugin/gamipress/reviews/?rate=5#new-post',
790 + '&#9733;&#9733;&#9733;&#9733;&#9733;'
791 + );
792 +
793 + return str_replace( '</span>', '', $footer_text ) . ' | ' . $gamipress_footer_text . '</span>';
794 +
795 + } else {
796 +
797 + return $footer_text;
798 +
799 + }
800 +
801 +}
802 +add_filter( 'admin_footer_text', 'gamipress_admin_footer_text' );