Support

Account

Home Forums Add-ons Repeater Field Trouble to use update_sub_field() for saving specific user ID?

Unread

Trouble to use update_sub_field() for saving specific user ID?

    • Aveeno

    • September 25, 2020 at 11:12 pm

    I have been trying to Googling around and I can’t really find any answers for this.

    I am creating a simple LMS project. The idea is that there will be many users. Each user has their own user ID which is generated from the WP.

    Each user will have one or many courses displayed on the website, and on the website, the user can click on the checkboxes to mark which of the course lecture they have finished.

    The problem is that right now the checkboxes are “global state”. That means every user can mark the checkbox and it’s not private anymore, or rather marked specifically for the user who attended the course lecture.

    I have managed to create my own WP Customized REST API, but I have a problem with how to use the update_sub_field() in context to update for a specific user ID.

    I am using the repeater Field type with checkboxes.

    Here is my code:

    
    add_action('rest_api_init', 's68_course_api');
    
    function s68_course_api() {
        register_rest_route('s68/v1', 'course/(?P<id>\d+)', [
          [
            'methods' => WP_REST_Server::EDITABLE,
            'callback' => 's68_update_course'
          ]
        ]);
    }
    
    function s68_update_course($data) {
          $subfield_whitelist = ['academy_course_progress_button'];
        $post_id = sanitize_text_field( $data['id' ] );
    
        if( $data['course_content'] ) {
            $row_index = intval( $data['course_content']['row_index'] );
            $sub_field = sanitize_text_field( $data['course_content']['sub_field'] );
            $new_value = $data['course_content']['value'];
    
            // If intval() returned 0, then $data['course_content']['row_index'] was not a number.
            // It's alright to check this way because ACF row indexes start at 1 instead of 0 - so 0 is
            // never a valid row number.
            if( $row_index === 0 ) {
                return [
                    'success' => false,
                    'message' => 'Invalid row number'
                ];
            }
    
            // Don't let malicious users change subfields they're not allowed to
            if( !in_array( $sub_field, $subfield_whitelist ) ) {
                return [
                    'success' => false,
                    'message' => 'Invalid sub field'
                ];
            }
    
            $success = update_sub_field(
              [ 'academy_course_content', $row_index, $sub_field ],
              $new_value,
              $post_id,
            );
    
            update_user_meta('user_' . 1, '', $success);
    
            return [
                'success' => $success
            ];
        }
    
        return [
            'success' => false,
            'message' => 'Unknown update request'
        ];
    }
    

    I am not sure what I am missing here.

    How do I achieve this?

    Much appreciate if someone could please help me out

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.