blocks
10 months ago
blocks.php
1 year ago
optimizer.php
1 year ago
safe-svg-attributes.php
3 years ago
safe-svg-settings.php
1 year ago
safe-svg-tags.php
3 years ago
safe-svg-settings.php
194 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Safe SVG plugin settings. |
| 4 | * |
| 5 | * @package safe-svg |
| 6 | */ |
| 7 | |
| 8 | namespace SafeSvg; |
| 9 | |
| 10 | /** |
| 11 | * SVG settings class. |
| 12 | */ |
| 13 | class safe_svg_settings { |
| 14 | |
| 15 | /** |
| 16 | * Set up the class |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | add_action( 'admin_init', [ $this, 'settings_init' ] ); |
| 20 | add_filter( 'pre_update_option_safe_svg_upload_roles', [ $this, 'update_capability' ], 10, 2 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Custom option and settings |
| 25 | */ |
| 26 | public function settings_init() { |
| 27 | register_setting( 'media', 'safe_svg_upload_roles', [ $this, 'sanitize_safe_svg_roles' ] ); |
| 28 | register_setting( |
| 29 | 'media', |
| 30 | 'safe_svg_large_svg', |
| 31 | [ |
| 32 | 'type' => 'integer', |
| 33 | 'default' => 0, |
| 34 | 'sanitize_callback' => 'absint', |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | add_settings_section( |
| 39 | 'safe_svg_settings', |
| 40 | __( 'Safe SVG Settings', 'safe-svg' ), |
| 41 | [ $this, 'safe_svg_settings_callback' ], |
| 42 | 'media' |
| 43 | ); |
| 44 | |
| 45 | add_settings_field( |
| 46 | 'safe_svg_roles', |
| 47 | __( 'User Roles', 'safe-svg' ), |
| 48 | [ $this, 'safe_svg_roles_cb' ], |
| 49 | 'media', |
| 50 | 'safe_svg_settings' |
| 51 | ); |
| 52 | |
| 53 | add_settings_field( |
| 54 | 'safe_svg_large_svg', |
| 55 | __( 'Large Files', 'safe-svg' ), |
| 56 | [ $this, 'safe_svg_large_svg_cb' ], |
| 57 | 'media', |
| 58 | 'safe_svg_settings' |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Sanitizes roles before saving. |
| 64 | * |
| 65 | * @param array $roles The roles that we are attempting to save |
| 66 | * |
| 67 | * @return array The sanitized roles array. |
| 68 | */ |
| 69 | public function sanitize_safe_svg_roles( $roles ) { |
| 70 | if ( ! is_array( $roles ) ) { |
| 71 | $roles = []; |
| 72 | } |
| 73 | |
| 74 | $valid_roles = $this->get_upload_capable_roles(); |
| 75 | $valid_slugs = array_keys( $valid_roles ); |
| 76 | $roles = array_intersect( $valid_slugs, $roles ); |
| 77 | |
| 78 | // Store a non empty/falsy value for easier handling. |
| 79 | if ( empty( $roles ) ) { |
| 80 | $roles = 'none'; |
| 81 | } |
| 82 | |
| 83 | return $roles; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get roles with upload capabilities. |
| 88 | * |
| 89 | * @return array An array of roles with the upload_files capability. |
| 90 | */ |
| 91 | public function get_upload_capable_roles() { |
| 92 | $all_roles = get_editable_roles(); |
| 93 | $upload_roles = array_filter( |
| 94 | $all_roles, |
| 95 | function( $_role ) { |
| 96 | return $_role['capabilities']['upload_files'] ?? false; |
| 97 | } |
| 98 | ); |
| 99 | |
| 100 | /** |
| 101 | * Filter the roles that can upload SVG files. |
| 102 | * |
| 103 | * @since 2.2.0 |
| 104 | * |
| 105 | * @param array $upload_roles The roles that can upload SVG files. |
| 106 | * @param array $all_roles All editable roles on the site. |
| 107 | * @param safe_svg_settings $this The safe_svg_settings instance. |
| 108 | */ |
| 109 | return apply_filters( 'safe_svg_upload_roles', $upload_roles, $all_roles, $this ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Settings section callback function. |
| 114 | * |
| 115 | * @param array $args The settings array, defining title, id, callback. |
| 116 | */ |
| 117 | public function safe_svg_settings_callback( $args ) { |
| 118 | ?> |
| 119 | <p id="<?php echo esc_attr( $args['id'] ); ?>"> |
| 120 | <?php esc_html_e( 'Select which user roles can upload SVG files.', 'safe-svg' ); ?> |
| 121 | </p> |
| 122 | <?php |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * User role field callback function. |
| 127 | */ |
| 128 | public function safe_svg_roles_cb() { |
| 129 | $upload_roles = (array) get_option( 'safe_svg_upload_roles', [] ); |
| 130 | $role_options = $this->get_upload_capable_roles(); |
| 131 | |
| 132 | if ( empty( $upload_roles ) ) { |
| 133 | $upload_roles = array_keys( $role_options ); |
| 134 | } |
| 135 | |
| 136 | foreach ( $role_options as $role => $info ) : |
| 137 | ?> |
| 138 | <div> |
| 139 | <label> |
| 140 | <input type="checkbox" name="safe_svg_upload_roles[]" value="<?php echo esc_attr( $role ); ?>" <?php checked( in_array( $role, $upload_roles, true ), true ); ?> /> <?php echo esc_html( $info['name'] ); ?> |
| 141 | </label> |
| 142 | </div> |
| 143 | <?php |
| 144 | endforeach; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Update user role capability based on the settings. |
| 149 | * |
| 150 | * @param array $new_roles New user roles. |
| 151 | * @param array $old_roles Old user roles. |
| 152 | * |
| 153 | * @return array |
| 154 | */ |
| 155 | public function update_capability( $new_roles, $old_roles ) { |
| 156 | $add_roles = array_filter( array_diff( (array) $new_roles, (array) $old_roles ) ); |
| 157 | $remove_roles = array_filter( array_diff( (array) $old_roles, (array) $new_roles ) ); |
| 158 | |
| 159 | if ( ! empty( $add_roles ) ) { |
| 160 | foreach ( $add_roles as $role ) { |
| 161 | $role = get_role( $role ); |
| 162 | |
| 163 | if ( $role instanceof \WP_Role ) { |
| 164 | $role->add_cap( 'safe_svg_upload_svg' ); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if ( ! empty( $remove_roles ) ) { |
| 170 | foreach ( $remove_roles as $role ) { |
| 171 | $role = get_role( $role ); |
| 172 | |
| 173 | if ( $role instanceof \WP_Role ) { |
| 174 | $role->remove_cap( 'safe_svg_upload_svg' ); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return $new_roles; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Large SVG files field callback function. |
| 184 | */ |
| 185 | public function safe_svg_large_svg_cb() { |
| 186 | ?> |
| 187 | <label> |
| 188 | <input type="checkbox" name="safe_svg_large_svg" value="1" <?php checked( get_option( 'safe_svg_large_svg' ), 1 ); ?> /> <?php esc_html_e( 'Allow large SVG files', 'safe-svg' ); ?> |
| 189 | </label> |
| 190 | <p class="description"><?php esc_html_e( 'Turning this on will allow SVG files larger than 10MB to be uploaded. This can impact performance and is not recommended unless needed.', 'safe-svg' ); ?></p> |
| 191 | <?php |
| 192 | } |
| 193 | } |
| 194 |