`). */ final class Security { /** * The actions and their default capabilities. */ const ACTIONS = array( 'screen', 'settings', 'records' ); /** * The capability one action needs. * * @param string $action One of ACTIONS. * @return string */ public static function capability( $action ) { if ( ! in_array( $action, self::ACTIONS, true ) ) { return 'manage_options'; } $default = 'records' === $action && class_exists( 'WooCommerce' ) ? 'manage_woocommerce' : 'manage_options'; /** * Filters the capability one Agevow action needs: `agevow_capability_screen` * (open the Agevow screen), `agevow_capability_settings` (change the settings * and turn the gate on or off), `agevow_capability_records` (read the checks, * the counts and the history). * * @param string $capability The default. */ $cap = apply_filters( 'agevow_capability_' . $action, $default ); return is_string( $cap ) && '' !== $cap ? $cap : $default; } /** * A REST error with its status. * * @param string $code Machine code. * @param string $message Plain words for the owner. * @param int $status HTTP status. * @return \WP_Error */ public static function error( $code, $message, $status ) { return new \WP_Error( $code, $message, array( 'status' => (int) $status ) ); } /** * Whether the request carries a valid REST nonce for the current user. * * WordPress already drops a cookie session that sends no nonce; this checks it * again on every admin route of ours, so a request authenticated some other way * never reaches a route without one. * * @param \WP_REST_Request $request Request. * @return bool */ public static function nonce_ok( \WP_REST_Request $request ) { $nonce = $request->get_header( 'X-WP-Nonce' ); if ( ! is_string( $nonce ) || '' === $nonce ) { $nonce = $request->get_param( '_wpnonce' ); } return is_string( $nonce ) && '' !== $nonce && false !== wp_verify_nonce( $nonce, 'wp_rest' ); } /** * Whether the current user may mark THIS product. * * @param int $id Product id. * @return bool */ public static function can_edit_product( $id ) { return Restrictions::is_product( $id ) && current_user_can( 'edit_product', (int) $id ); } /** * Whether the current user may mark THIS category. * * @param int $id Term id. * @return bool */ public static function can_edit_term( $id ) { return Restrictions::is_term( $id ) && current_user_can( 'edit_term', (int) $id ); } }