actions as $action => $details ) { add_action( 'admin_post_' . $this->namespace . '/' . $action, array( $this, 'handle_admin_post_request' ) ); } } public function handle_admin_post_request() { $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; $action = explode( $this->namespace . '/', $action )[1]; if ( ! isset( $this->actions[ $action ] ) ) { wp_die( esc_html__( 'Invalid POST action.', 'academy' ) ); } $details = $this->actions[ $action ]; $nonce = isset( $_REQUEST['security'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['security'] ) ) : ''; if ( empty( $nonce ) && isset( $_REQUEST['_wpnonce'] ) ) { $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); } if ( ! wp_verify_nonce( $nonce, $this->nonce_action ) ) { wp_die( esc_html__( 'Invalid nonce.', 'academy' ) ); } if ( ! is_user_logged_in() || ! current_user_can( isset( $details['capability'] ) ? $details['capability'] : 'manage_options' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'academy' ) ); } if ( is_callable( $details['callback'] ) ) { call_user_func( $details['callback'], $this->get_request_payload( $details ) ); } else { wp_die( esc_html__( 'Invalid callback method.', 'academy' ) ); } } /** * The data an action's callback receives. * * Every Academy action declares the fields it accepts ('payload' => schema, * or a callable returning one). Only those fields are read from the * request, and each is typed by Sanitizer::sanitize_payload() before the * callback runs. An Academy action that declares nothing receives nothing. * * @param array $details Action definition. * @return array */ protected function get_request_payload( $details ) { $schema = isset( $details['payload'] ) ? $details['payload'] : null; if ( is_callable( $schema ) ) { $schema = call_user_func( $schema ); } if ( is_array( $schema ) ) { return Sanitizer::sanitize_payload( $schema, Sanitizer::declared_request_fields( array_keys( $schema ) ) ); } if ( $this->is_academy_handler() ) { return array(); } // Handlers registered by other plugins (Academy add-ons) that do not // declare a payload yet keep receiving the request, cleaned of unsafe // keys, invalid UTF-8 and control characters; their callbacks type it. // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce verified above; sanitized by Sanitizer::sanitize_request_payload(). return Sanitizer::sanitize_request_payload( wp_unslash( $_POST ) ); } /** * Whether this handler is part of Academy LMS itself. * * @return bool */ protected function is_academy_handler() { $file = ( new \ReflectionClass( $this ) )->getFileName(); return $file && 0 === strpos( wp_normalize_path( $file ), wp_normalize_path( ACADEMY_ROOT_DIR_PATH ) ); } }