post_type ) { return false; } return in_array( get_post_mime_type( $post ), self::EDITABLE_MIMES, true ); } /** * Does the user hold one of the configured roles? Empty setting = any role. * * @param array $roles Allowed roles. * @param \WP_User|int $user User. * @return bool */ private static function has_role( $roles, $user = null ) { if ( empty( $roles ) ) { return true; } $user = $user ? ( is_object( $user ) ? $user : get_userdata( $user ) ) : wp_get_current_user(); if ( ! $user || ! $user->exists() ) { return false; } if ( is_multisite() && is_super_admin( $user->ID ) ) { return true; } return (bool) array_intersect( (array) $user->roles, (array) $roles ); } /** * Can the current user open this attachment in the editor? * * @param int $id Attachment ID. * @return bool */ public static function can_edit( $id ) { $ok = is_user_logged_in() && self::is_editable_image( $id ) && current_user_can( 'upload_files' ) && current_user_can( 'edit_post', $id ) && self::has_role( Settings::get( 'roles' ) ); /** * Filter whether the current user may open an attachment in PictureEditor. * * @param bool $ok Decision. * @param int $id Attachment ID. */ return (bool) apply_filters( 'pictureeditor_can_edit', $ok, $id ); } /** * Can the current user save an edited copy as a new attachment? * * @param int $id Source attachment ID. * @return bool */ public static function can_save_new( $id ) { return self::can_edit( $id ) && current_user_can( 'upload_files' ); } /** * Can the current user replace this attachment's file? Separate, stricter permission (ยง12.2). * * @param int $id Attachment ID. * @return bool */ public static function can_replace( $id ) { $ok = self::can_edit( $id ) && Settings::get( 'replace_enabled' ) && current_user_can( 'edit_post', $id ) && current_user_can( 'delete_post', $id ) && self::has_role( Settings::get( 'replace_roles' ) ); /** * Filter whether the current user may replace an attachment's file. * * @param bool $ok Decision. * @param int $id Attachment ID. */ return (bool) apply_filters( 'pictureeditor_can_replace', $ok, $id ); } /** * Permissions object for the editor (EditorPermissions contract). * * @param int $id Attachment ID. * @return array */ public static function for_editor( $id ) { $replace_ok = self::can_replace( $id ); return array( 'canEdit' => self::can_edit( $id ), 'canSaveNew' => self::can_save_new( $id ), 'canReplace' => $replace_ok, 'canBatch' => (bool) apply_filters( 'pictureeditor_can_batch', false, $id ), ); } }