| @@ -1,0 +1,112 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\armember\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Overrides GamiPress AJAX Helper for selecting posts | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_armember_ajax_get_posts() { | |
| 18 | + | |
| 19 | + // Security check, forces to die if not security passed | |
| 20 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 21 | + | |
| 22 | + // Check if user can manage GamiPress | |
| 23 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 24 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + global $wpdb; | |
| 28 | + | |
| 29 | + $results = array(); | |
| 30 | + | |
| 31 | + // Pull back the search string | |
| 32 | + $search = isset( $_REQUEST['q'] ) ? sanitize_text_field( $_REQUEST['q'] ) : ''; | |
| 33 | + $search = $wpdb->esc_like( $search ); | |
| 34 | + | |
| 35 | + if( isset( $_REQUEST['post_type'] ) && in_array( 'armember_membership', $_REQUEST['post_type'] ) ) { | |
| 36 | + | |
| 37 | + // Get the membership plans | |
| 38 | + $memberships = gamipress_armember_get_plan(); | |
| 39 | + | |
| 40 | + foreach ( $memberships as $plan ) { | |
| 41 | + | |
| 42 | + // Results should meet same structure like posts | |
| 43 | + $results[] = array( | |
| 44 | + 'ID' => $plan['id'], | |
| 45 | + 'post_title' => $plan['name'], | |
| 46 | + ); | |
| 47 | + | |
| 48 | + } | |
| 49 | + | |
| 50 | + // Return our results | |
| 51 | + wp_send_json_success( $results ); | |
| 52 | + die; | |
| 53 | + | |
| 54 | + } | |
| 55 | + | |
| 56 | +} | |
| 57 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_armember_ajax_get_posts', 5 ); | |
| 58 | + | |
| 59 | +/** | |
| 60 | + * Get Membership plan | |
| 61 | + * | |
| 62 | + * @since 1.0.0 | |
| 63 | + * | |
| 64 | + * @param int $plan_id | |
| 65 | + * | |
| 66 | + * @return string|null | |
| 67 | + */ | |
| 68 | +function gamipress_armember_get_plan( ) { | |
| 69 | + | |
| 70 | + $membership_plans = array(); | |
| 71 | + | |
| 72 | + if( class_exists( 'ARM_subscription_plans' ) ) { | |
| 73 | + $obj_plans = new \ARM_subscription_plans(); | |
| 74 | + } else { | |
| 75 | + $obj_plans = new \ARM_subscription_plans_Lite(); | |
| 76 | + } | |
| 77 | + | |
| 78 | + $all_plans = $obj_plans->arm_get_all_subscription_plans( 'arm_subscription_plan_id, arm_subscription_plan_name' ); | |
| 79 | + | |
| 80 | + foreach ( $all_plans as $plan ){ | |
| 81 | + | |
| 82 | + $membership_plans[] = array( | |
| 83 | + 'id' => $plan['arm_subscription_plan_id'], | |
| 84 | + 'name' => $plan['arm_subscription_plan_name'], | |
| 85 | + ); | |
| 86 | + | |
| 87 | + } | |
| 88 | + | |
| 89 | + return $membership_plans; | |
| 90 | + | |
| 91 | +} | |
| 92 | + | |
| 93 | +// Get the membership plan title | |
| 94 | +function gamipress_armember_get_plan_title( $plan_id ) { | |
| 95 | + | |
| 96 | + // Empty title if no ID provided | |
| 97 | + if( absint( $plan_id ) === 0 ) { | |
| 98 | + return ''; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if( class_exists( 'ARM_subscription_plans' ) ) { | |
| 102 | + $obj_plans = new \ARM_subscription_plans(); | |
| 103 | + } else { | |
| 104 | + $obj_plans = new \ARM_subscription_plans_Lite(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + $plan_name = $obj_plans->arm_get_plan_name_by_id( $plan_id ); | |
| 108 | + | |
| 109 | + return $plan_name; | |
| 110 | + | |
| 111 | +} | |
| 112 | + | |