'', 'post_id' => 0, 'context' => 'media-library', 'user_role' => self::get_primary_role(), ); // Detect post type from post ID. if ( $post_id ) { $context['post_id'] = (int) $post_id; $context['post_type'] = get_post_type( $post_id ); } elseif ( ! empty( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $context['post_id'] = (int) $_GET['post']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $context['post_type'] = get_post_type( $context['post_id'] ); } // Detect from current screen if available. $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen ) { if ( ! empty( $screen->post_type ) && empty( $context['post_type'] ) ) { $context['post_type'] = $screen->post_type; } } return $context; } /** * Build context from an AJAX request. * * Reads context data sent by the frontend JavaScript. * All values are sanitized. * * @since 1.2.0 * @return array Context data. */ public static function get_ajax_context() { $context = array( 'post_type' => '', 'post_id' => 0, 'context' => 'media-library', 'user_role' => self::get_primary_role(), ); // Read from POST data (sent by frontend). phpcs:ignore WordPress.Security.NonceVerification -- nonce verified in calling handler. if ( ! empty( $_POST['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $context['post_type'] = sanitize_key( wp_unslash( $_POST['post_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } if ( ! empty( $_POST['post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $context['post_id'] = (int) $_POST['post_id']; // phpcs:ignore WordPress.Security.NonceVerification.Missing // Auto-detect post type from post ID if not provided. if ( empty( $context['post_type'] ) && $context['post_id'] > 0 ) { $context['post_type'] = get_post_type( $context['post_id'] ); } } if ( ! empty( $_POST['upload_context'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $upload_context = sanitize_key( wp_unslash( $_POST['upload_context'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( in_array( $upload_context, GATEWELL_Rule_Storage::VALID_CONTEXTS, true ) ) { $context['context'] = $upload_context; } } return $context; } /** * Get the primary role of the current user. * * WordPress users can have multiple roles. This returns the first role * which is typically the primary one. * * @since 1.2.0 * @return string Primary role slug, or 'subscriber' as fallback. */ public static function get_primary_role() { $user = wp_get_current_user(); if ( ! $user || ! $user->exists() ) { return ''; } $roles = $user->roles; if ( ! empty( $roles ) ) { return reset( $roles ); } return 'subscriber'; } /** * Build context data to pass to JavaScript via wp_localize_script. * * @since 1.2.0 * @param int|null $post_id Optional post ID. * @return array Context data for JavaScript. */ public static function get_js_context( $post_id = null ) { $context = self::get_current_context( $post_id ); return array( 'postType' => $context['post_type'], 'postId' => $context['post_id'], 'userRole' => $context['user_role'], ); } }