actions = array( 'get_membership' => array( 'callback' => array( $this, 'get_membership' ), 'payload' => array( 'course_id' => 'integer', ), 'capability' => 'manage_academy_instructor', ), 'save_membership' => array( 'callback' => array( $this, 'save_membership' ), 'payload' => array( 'course_id' => 'integer', 'access_group' => 'json_string', ), 'capability' => 'manage_academy_instructor', ), ); } public function get_membership( array $args ) { $args['course_id'] = absint( $args['course_id'] ?? 0 ); if ( ! $args['course_id'] ) { wp_send_json_error( array( 'message' => esc_html__( 'Course ID is required.', 'academy' ), ) ); } if ( ! \Academy\Helper::current_user_can_manage_course( $args['course_id'] ) ) { wp_send_json_error( array( 'message' => esc_html__( 'Sorry, you are not allowed to manage this course.', 'academy' ) ), 403 ); } $membership_id = get_post_meta( $args['course_id'], 'academy_store_membership', true ); if ( ! $membership_id ) { wp_send_json_error( array( 'message' => esc_html__( 'No membership found.', 'academy' ), ) ); } $group_content = maybe_unserialize( get_post_meta( $membership_id, '_storeengine_membership_content_protect_types', true ) ); $current_rule = 'post-' . $args['course_id'] . '-|'; if ( ! in_array( $current_rule, $this->specific_values( $group_content['specifics'] ?? [] ), true ) ) { wp_send_json_error( array( 'message' => esc_html__( 'No integration rules found.', 'academy' ), ) ); } wp_send_json_success( array( 'type' => 'membership', 'label' => get_the_title( $membership_id ), 'value' => $membership_id, ) ); } public function save_membership( array $args ) { $args['course_id'] = absint( $args['course_id'] ?? 0 ); if ( ! $args['course_id'] ) { wp_send_json_error( [ 'message' => esc_html__( 'Course is required.', 'academy' ), ] ); } if ( 'academy_courses' !== get_post_type( $args['course_id'] ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Course does not exist.', 'academy' ), ] ); } if ( ! \Academy\Helper::current_user_can_manage_course( $args['course_id'] ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Sorry, you are not allowed to manage this course.', 'academy' ) ], 403 ); } if ( $args['course_id'] && empty( $args['access_group'] ) ) { $this->handle_remove_request( $args['course_id'] ); } if ( ! $args['access_group'] ) { wp_send_json_error( [ 'message' => esc_html__( 'Access group is required.', 'academy' ), ] ); } if ( ! $args['course_id'] ) { wp_send_json_error( [ 'message' => esc_html__( 'Course is required.', 'academy' ), ] ); } $access_group = json_decode( (string) $args['access_group'] ); $group_id = is_object( $access_group ) && isset( $access_group->value ) ? absint( $access_group->value ) : 0; if ( ! $group_id || ! get_post( $group_id ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Access group is not found.', 'academy' ), ] ); } // Only these sanitized fields are stored or echoed back. $access_group = (object) array( 'value' => $group_id, 'label' => isset( $access_group->label ) ? sanitize_text_field( (string) $access_group->label ) : get_the_title( $group_id ), ); $integration = Helper::get_integration_repository_by_id( 'storeengine/membership-addon', $group_id ); if ( empty( $integration ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Access group is not integrated with any product.', 'academy' ), ] ); } $group_content = get_post_meta( $group_id, '_storeengine_membership_content_protect_types', true ); if ( ! $group_content ) { wp_send_json_error( [ 'message' => esc_html__( 'Access group is not found.', 'academy' ), ] ); } $group_content = (array) maybe_unserialize( $group_content ); $group_content['rules'] = isset( $group_content['rules'] ) ? (array) $group_content['rules'] : array(); if ( ! in_array( 'specifics', $group_content['rules'], true ) ) { $group_content['rules'][] = 'specifics'; } $is_already_specified = $this->handle_specifics_duplicate( $args['course_id'], $group_content['specifics'] ?? [] ); if ( true === $is_already_specified ) { wp_send_json_error( [ 'message' => esc_html__( 'Access group already added to this course.', 'academy' ), ] ); } // Store the same shape StoreEngine itself uses for a specific content // rule ( { value, label } ); a bare string breaks StoreEngine's access // group editor and this addon's own duplicate/remove matching. $group_content['specifics'][] = [ 'value' => 'post-' . $args['course_id'] . '-|', 'label' => get_the_title( $args['course_id'] ) . ' - ' . str_replace( '_', ' ', get_post_type( $args['course_id'] ) ), ]; update_post_meta( $group_id, '_storeengine_membership_content_protect_types', $group_content ); update_post_meta( $args['course_id'], 'academy_store_membership', $group_id ); $access_group->type = 'membership'; wp_send_json_success( [ 'message' => esc_html__( 'Access group updated successfully.', 'academy' ), 'data' => $access_group, ] ); } public function handle_remove_request( $course_id ) { $membership_id = get_post_meta( $course_id, 'academy_store_membership', true ); if ( ! $membership_id ) { wp_send_json_error( array( [ 'message' => esc_html__( 'No membership founds.', 'academy' ), ] ) ); } $group_content = maybe_unserialize( get_post_meta( $membership_id, '_storeengine_membership_content_protect_types', true ) ); $group_content['specifics'] = $this->handle_specifics_duplicate( $course_id, $group_content['specifics'] ?? [], true ); update_post_meta( $membership_id, '_storeengine_membership_content_protect_types', $group_content ); delete_post_meta( $course_id, 'academy_store_membership' ); wp_send_json_success( array( 'message' => esc_html__( 'Membership removed successfully.', 'academy' ), ) ); } public function handle_specifics_duplicate( $course_id, $specifics, $is_remove = false ) { $current_membership = 'post-' . $course_id . '-|'; foreach ( (array) $specifics as $key => $specific ) { // Tolerate both shapes: the { value, label } StoreEngine stores and // any bare-string entry an older build of this addon may have left. $value = is_array( $specific ) ? ( $specific['value'] ?? '' ) : $specific; if ( $value === $current_membership ) { if ( $is_remove ) { unset( $specifics[ $key ] ); } else { return true; } } } return $is_remove ? array_values( $specifics ) : $specifics; } /** * Flatten a specifics list to its rule values, tolerating both the * { value, label } shape StoreEngine stores and any legacy bare strings. * * @param mixed $specifics * * @return string[] */ protected function specific_values( $specifics ): array { return array_map( static function ( $specific ) { return is_array( $specific ) ? ( $specific['value'] ?? '' ) : $specific; }, (array) $specifics ); } }