get( 'allowed_roles', array() ); if ( empty( $allowed ) ) { return current_user_can( 'edit_posts' ); } $user = wp_get_current_user(); if ( ! $user || ! $user->exists() ) { return false; } foreach ( (array) $user->roles as $role ) { if ( in_array( $role, $allowed, true ) ) { // Still require a base editing cap so subscribers never pass. if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_products' ) ) { return true; } } } return false; } /** * Can the current user edit this specific column? * Non-admins are blocked from Settings -> Protected columns. * * @param string $column Column key. * @return bool */ public static function can_edit_column( $column ) { if ( current_user_can( 'manage_options' ) ) { return true; } $settings = new Settings(); $protected = (array) $settings->get( 'protected_columns', array() ); return ! in_array( $column, $protected, true ); } /** * Can the current user open the list of this post type? * * @param string $post_type Post type. * @return bool */ public static function can_edit_type( $post_type ) { if ( ! self::can_use() ) { return false; } $obj = get_post_type_object( $post_type ); if ( ! $obj ) { return false; } if ( ! empty( $obj->cap->edit_posts ) && current_user_can( $obj->cap->edit_posts ) ) { return true; } // Fallback for types without edit_posts cap. return current_user_can( 'edit_posts' ); } /** * Can the current user edit this specific item? * * @param int $post_id Post ID. * @return bool */ public static function can_edit_post( $post_id ) { $post = get_post( absint( $post_id ) ); if ( ! $post || ! self::can_edit_type( $post->post_type ) ) { return false; } return current_user_can( 'edit_post', $post->ID ) && self::own_only_allows( $post ); } /** * Can the current user trash, restore or delete this specific item? * * @param int $post_id Post ID. * @return bool */ public static function can_delete_post( $post_id ) { $post = get_post( absint( $post_id ) ); if ( ! $post || ! self::can_edit_type( $post->post_type ) ) { return false; } return current_user_can( 'delete_post', $post->ID ) && self::own_only_allows( $post ); } /** * Can the current user write this custom field (meta:KEY column) of an item? * Protected fields (is_protected_meta(), usually a leading underscore) are * internal data of WordPress and other plugins: administrators may edit * them; other users only the SEO fields and columns the sheet itself offers * (ACF and others) or fields WordPress allows through edit_post_meta. * * @param int $post_id Post ID. * @param string $meta_key Meta key. * @return bool */ public static function can_edit_meta( $post_id, $meta_key ) { if ( ! is_protected_meta( $meta_key, 'post' ) || current_user_can( 'manage_options' ) ) { return true; } if ( in_array( $meta_key, Columns::SEO_META_KEYS, true ) ) { return true; } $post = get_post( absint( $post_id ) ); if ( $post && isset( Columns::get_columns( $post->post_type )[ 'meta:' . $meta_key ] ) ) { return true; } return current_user_can( 'edit_post_meta', absint( $post_id ), $meta_key ); } /** * Can the current user publish items of this type? * * @param string $post_type Post type. * @return bool */ public static function can_publish_type( $post_type ) { $obj = get_post_type_object( $post_type ); return $obj && current_user_can( $obj->cap->publish_posts ); } /** * Can the current user see and edit items of other authors in this type? * * @param string $post_type Post type. * @return bool */ public static function can_edit_others( $post_type ) { if ( current_user_can( 'manage_options' ) ) { return true; } $settings = new Settings(); if ( $settings->get( 'own_only', 0 ) ) { return false; } $obj = get_post_type_object( $post_type ); return $obj && current_user_can( $obj->cap->edit_others_posts ); } /** * Settings -> "Non-admins only see and edit their own items". * * @param \WP_Post $post Post. * @return bool */ private static function own_only_allows( $post ) { if ( current_user_can( 'manage_options' ) ) { return true; } $settings = new Settings(); return ! $settings->get( 'own_only', 0 ) || (int) $post->post_author === get_current_user_id(); } /** * Run a background task (cron) as the administrator who configured it. * Nothing runs when that user is gone or is no longer an administrator. * * @param int $user_id Owner user ID. * @param callable $task Task. * @return mixed|\WP_Error Task result. */ public static function as_admin( int $user_id, callable $task ) { $user = $user_id ? get_user_by( 'id', $user_id ) : false; if ( ! $user || ! user_can( $user, 'manage_options' ) ) { return new \WP_Error( 'rafgrid_no_owner', __( 'The administrator who set up this task no longer exists or is no longer an administrator. Save it again as an administrator.', 'rafgrid' ) ); } $previous = get_current_user_id(); wp_set_current_user( $user->ID ); try { return $task(); } finally { wp_set_current_user( $previous ); } } }