| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\GamiPress\Functions | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Options callback for select fields assigned to points types | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param stdClass $field | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function automatorwp_gamipress_points_types_options_cb( $field ) { | |
| 22 | + | |
| 23 | + // Setup vars | |
| 24 | + $value = $field->escaped_value; | |
| 25 | + $none_value = 'any'; | |
| 26 | + $none_label = __( 'any points type', 'automatorwp' ); | |
| 27 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 28 | + | |
| 29 | + foreach( gamipress_get_points_types() as $points_type => $data ) { | |
| 30 | + $options[$points_type] = $data['plural_name']; | |
| 31 | + } | |
| 32 | + | |
| 33 | + return $options; | |
| 34 | + | |
| 35 | +} | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Switch to main site if GamiPress is network wide active | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + */ | |
| 42 | +function automatorwp_gamipress_ajax_get_posts() { | |
| 43 | + | |
| 44 | + if( isset( $_REQUEST['post_type_cb'] ) ) { | |
| 45 | + | |
| 46 | + $post_type_cb = sanitize_text_field( $_REQUEST['post_type_cb'] ); | |
| 47 | + $gamipress_cbs = array( 'gamipress_get_points_types_slugs', 'gamipress_get_achievement_types_slugs', 'gamipress_get_rank_types_slugs' ); | |
| 48 | + | |
| 49 | + // If is any of the GamiPress post types | |
| 50 | + if( in_array( $post_type_cb, $gamipress_cbs ) ) { | |
| 51 | + gamipress_switch_to_main_site_if_network_wide_active(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + } | |
| 55 | + | |
| 56 | +} | |
| 57 | +add_action( 'wp_ajax_automatorwp_get_posts', 'automatorwp_gamipress_ajax_get_posts', 1 ); | |
| 58 | + | |
| 59 | +/** | |
| 60 | + * Utility function to get the post option parameter | |
| 61 | + * | |
| 62 | + * @since 1.0.0 | |
| 63 | + * | |
| 64 | + * @param array $args | |
| 65 | + * | |
| 66 | + * @return array | |
| 67 | + */ | |
| 68 | +function automatorwp_gamipress_utilities_post_option( $args = array() ) { | |
| 69 | + | |
| 70 | + $post_options = automatorwp_utilities_post_option( $args ); | |
| 71 | + | |
| 72 | + $post_options['fields']['post']['options_cb'] = 'automatorwp_gamipress_options_cb_posts'; | |
| 73 | + | |
| 74 | + return $post_options; | |
| 75 | + | |
| 76 | +} | |
| 77 | + | |
| 78 | +/** | |
| 79 | + * Options callback for select2 fields assigned to posts with the special condition that if is for GamiPress, requires a blog switch | |
| 80 | + * | |
| 81 | + * @since 1.0.0 | |
| 82 | + * | |
| 83 | + * @param stdClass $field | |
| 84 | + * | |
| 85 | + * @return array | |
| 86 | + */ | |
| 87 | +function automatorwp_gamipress_options_cb_posts( $field ) { | |
| 88 | + | |
| 89 | + if( gamipress_is_network_wide_active() ) { | |
| 90 | + gamipress_switch_to_main_site(); | |
| 91 | + } | |
| 92 | + | |
| 93 | + $options = automatorwp_options_cb_posts( $field ); | |
| 94 | + | |
| 95 | + if( gamipress_is_network_wide_active() ) { | |
| 96 | + restore_current_blog(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + return $options; | |
| 100 | + | |
| 101 | +} | |