@@ -18,758 +18,758 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class GetPaid_REST_Settings_Controller extends GetPaid_REST_Controller { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * An array of available settings. |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $settings; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Route base. |
|
| 30 | - * |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - protected $rest_base = 'settings'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Registers the routes for the objects of the controller. |
|
| 37 | - * |
|
| 38 | - * @since 2.0.0 |
|
| 39 | - * |
|
| 40 | - * @see register_rest_route() |
|
| 41 | - */ |
|
| 42 | - public function register_namespace_routes( $namespace ) { |
|
| 43 | - |
|
| 44 | - // List all registered tabs. |
|
| 45 | - register_rest_route( |
|
| 46 | - $namespace, |
|
| 47 | - $this->rest_base, |
|
| 48 | - array( |
|
| 49 | - array( |
|
| 50 | - 'methods' => WP_REST_Server::READABLE, |
|
| 51 | - 'callback' => array( $this, 'get_tabs' ), |
|
| 52 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 53 | - ), |
|
| 54 | - 'schema' => '__return_empty_array', |
|
| 55 | - ) |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - // View/Update a single setting. |
|
| 59 | - register_rest_route( |
|
| 60 | - $namespace, |
|
| 61 | - $this->rest_base . '/setting/(?P<id>[\w-]+)', |
|
| 62 | - array( |
|
| 63 | - 'args' => array( |
|
| 64 | - 'id' => array( |
|
| 65 | - 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
| 66 | - 'type' => 'string', |
|
| 67 | - 'required' => true, |
|
| 68 | - ), |
|
| 69 | - ), |
|
| 70 | - array( |
|
| 71 | - 'methods' => WP_REST_Server::READABLE, |
|
| 72 | - 'callback' => array( $this, 'get_item' ), |
|
| 73 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 74 | - ), |
|
| 75 | - array( |
|
| 76 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 77 | - 'callback' => array( $this, 'update_item' ), |
|
| 78 | - 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
| 79 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 80 | - ), |
|
| 81 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 82 | - ) |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - // List registered sections for a given tab. |
|
| 86 | - register_rest_route( |
|
| 87 | - $namespace, |
|
| 88 | - $this->rest_base . '/(?P<tab>[\w-]+)', |
|
| 89 | - array( |
|
| 90 | - 'args' => array( |
|
| 91 | - 'tab' => array( |
|
| 92 | - 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
| 93 | - 'type' => 'string', |
|
| 94 | - 'required' => true, |
|
| 95 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 96 | - ), |
|
| 97 | - ), |
|
| 98 | - array( |
|
| 99 | - 'methods' => WP_REST_Server::READABLE, |
|
| 100 | - 'callback' => array( $this, 'get_sections' ), |
|
| 101 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 102 | - ), |
|
| 103 | - 'schema' => '__return_empty_array', |
|
| 104 | - ) |
|
| 105 | - ); |
|
| 106 | - |
|
| 107 | - // List all registered settings for a given tab. |
|
| 108 | - register_rest_route( |
|
| 109 | - $namespace, |
|
| 110 | - $this->rest_base . '/(?P<tab>[\w-]+)/(?P<section>[\w-]+)', |
|
| 111 | - array( |
|
| 112 | - 'args' => array( |
|
| 113 | - 'tab' => array( |
|
| 114 | - 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 115 | - 'type' => 'string', |
|
| 116 | - 'required' => true, |
|
| 117 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 118 | - ), |
|
| 119 | - 'section' => array( |
|
| 120 | - 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 121 | - 'type' => 'string', |
|
| 122 | - 'required' => true, |
|
| 123 | - ), |
|
| 124 | - ), |
|
| 125 | - array( |
|
| 126 | - 'methods' => WP_REST_Server::READABLE, |
|
| 127 | - 'callback' => array( $this, 'get_items' ), |
|
| 128 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 129 | - ), |
|
| 130 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 131 | - ) |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - register_rest_route( |
|
| 135 | - $namespace, |
|
| 136 | - '/' . $this->rest_base . '/batch', |
|
| 137 | - array( |
|
| 138 | - 'args' => array( |
|
| 139 | - 'id' => array( |
|
| 140 | - 'description' => __( 'Setting ID.', 'invoicing' ), |
|
| 141 | - 'type' => 'string', |
|
| 142 | - ), |
|
| 143 | - ), |
|
| 144 | - array( |
|
| 145 | - 'methods' => WP_REST_Server::EDITABLE, |
|
| 146 | - 'callback' => array( $this, 'batch_items' ), |
|
| 147 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
| 148 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 149 | - ), |
|
| 150 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
| 151 | - ) |
|
| 152 | - ); |
|
| 153 | - |
|
| 154 | - } |
|
| 21 | + /** |
|
| 22 | + * An array of available settings. |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $settings; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Route base. |
|
| 30 | + * |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + protected $rest_base = 'settings'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Registers the routes for the objects of the controller. |
|
| 37 | + * |
|
| 38 | + * @since 2.0.0 |
|
| 39 | + * |
|
| 40 | + * @see register_rest_route() |
|
| 41 | + */ |
|
| 42 | + public function register_namespace_routes( $namespace ) { |
|
| 43 | + |
|
| 44 | + // List all registered tabs. |
|
| 45 | + register_rest_route( |
|
| 46 | + $namespace, |
|
| 47 | + $this->rest_base, |
|
| 48 | + array( |
|
| 49 | + array( |
|
| 50 | + 'methods' => WP_REST_Server::READABLE, |
|
| 51 | + 'callback' => array( $this, 'get_tabs' ), |
|
| 52 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 53 | + ), |
|
| 54 | + 'schema' => '__return_empty_array', |
|
| 55 | + ) |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + // View/Update a single setting. |
|
| 59 | + register_rest_route( |
|
| 60 | + $namespace, |
|
| 61 | + $this->rest_base . '/setting/(?P<id>[\w-]+)', |
|
| 62 | + array( |
|
| 63 | + 'args' => array( |
|
| 64 | + 'id' => array( |
|
| 65 | + 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
| 66 | + 'type' => 'string', |
|
| 67 | + 'required' => true, |
|
| 68 | + ), |
|
| 69 | + ), |
|
| 70 | + array( |
|
| 71 | + 'methods' => WP_REST_Server::READABLE, |
|
| 72 | + 'callback' => array( $this, 'get_item' ), |
|
| 73 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 74 | + ), |
|
| 75 | + array( |
|
| 76 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 77 | + 'callback' => array( $this, 'update_item' ), |
|
| 78 | + 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
| 79 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 80 | + ), |
|
| 81 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 82 | + ) |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + // List registered sections for a given tab. |
|
| 86 | + register_rest_route( |
|
| 87 | + $namespace, |
|
| 88 | + $this->rest_base . '/(?P<tab>[\w-]+)', |
|
| 89 | + array( |
|
| 90 | + 'args' => array( |
|
| 91 | + 'tab' => array( |
|
| 92 | + 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
| 93 | + 'type' => 'string', |
|
| 94 | + 'required' => true, |
|
| 95 | + 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 96 | + ), |
|
| 97 | + ), |
|
| 98 | + array( |
|
| 99 | + 'methods' => WP_REST_Server::READABLE, |
|
| 100 | + 'callback' => array( $this, 'get_sections' ), |
|
| 101 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 102 | + ), |
|
| 103 | + 'schema' => '__return_empty_array', |
|
| 104 | + ) |
|
| 105 | + ); |
|
| 106 | + |
|
| 107 | + // List all registered settings for a given tab. |
|
| 108 | + register_rest_route( |
|
| 109 | + $namespace, |
|
| 110 | + $this->rest_base . '/(?P<tab>[\w-]+)/(?P<section>[\w-]+)', |
|
| 111 | + array( |
|
| 112 | + 'args' => array( |
|
| 113 | + 'tab' => array( |
|
| 114 | + 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 115 | + 'type' => 'string', |
|
| 116 | + 'required' => true, |
|
| 117 | + 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 118 | + ), |
|
| 119 | + 'section' => array( |
|
| 120 | + 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 121 | + 'type' => 'string', |
|
| 122 | + 'required' => true, |
|
| 123 | + ), |
|
| 124 | + ), |
|
| 125 | + array( |
|
| 126 | + 'methods' => WP_REST_Server::READABLE, |
|
| 127 | + 'callback' => array( $this, 'get_items' ), |
|
| 128 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 129 | + ), |
|
| 130 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 131 | + ) |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + register_rest_route( |
|
| 135 | + $namespace, |
|
| 136 | + '/' . $this->rest_base . '/batch', |
|
| 137 | + array( |
|
| 138 | + 'args' => array( |
|
| 139 | + 'id' => array( |
|
| 140 | + 'description' => __( 'Setting ID.', 'invoicing' ), |
|
| 141 | + 'type' => 'string', |
|
| 142 | + ), |
|
| 143 | + ), |
|
| 144 | + array( |
|
| 145 | + 'methods' => WP_REST_Server::EDITABLE, |
|
| 146 | + 'callback' => array( $this, 'batch_items' ), |
|
| 147 | + 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
| 148 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 149 | + ), |
|
| 150 | + 'schema' => array( $this, 'get_public_batch_schema' ), |
|
| 151 | + ) |
|
| 152 | + ); |
|
| 153 | + |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Return all settings. |
|
| 158 | - * |
|
| 159 | - * @since 2.0.0 |
|
| 160 | - * @param WP_REST_Request $request Request data. |
|
| 161 | - * @return WP_Error|WP_REST_Response |
|
| 162 | - */ |
|
| 163 | - public function get_items( $request ) { |
|
| 164 | - |
|
| 165 | - $settings = $this->get_settings(); |
|
| 166 | - |
|
| 167 | - if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
| 168 | - return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
| 172 | - return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
| 176 | - $prepared = array(); |
|
| 177 | - |
|
| 178 | - foreach ( $settings as $setting ) { |
|
| 179 | - |
|
| 180 | - $setting = $this->sanitize_setting( $setting ); |
|
| 181 | - $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
| 182 | - $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
| 183 | - |
|
| 184 | - if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
| 185 | - $prepared[] = $setting_data; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return rest_ensure_response( $prepared ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Return a single setting. |
|
| 195 | - * |
|
| 196 | - * @since 2.0.0 |
|
| 197 | - * @param WP_REST_Request $request Request data. |
|
| 198 | - * @return WP_Error|WP_REST_Response |
|
| 199 | - */ |
|
| 200 | - public function get_item( $request ) { |
|
| 201 | - $setting = $this->get_setting( $request['id'] ); |
|
| 202 | - |
|
| 203 | - if ( is_wp_error( $setting ) ) { |
|
| 204 | - return $setting; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $setting = $this->sanitize_setting( $setting ); |
|
| 208 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 209 | - return rest_ensure_response( $response ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Update a single setting. |
|
| 214 | - * |
|
| 215 | - * @since 2.0.0 |
|
| 216 | - * @param WP_REST_Request $request Request data. |
|
| 217 | - * @return WP_Error|WP_REST_Response |
|
| 218 | - */ |
|
| 219 | - public function update_item( $request ) { |
|
| 220 | - $setting = $this->get_setting( $request['id'] ); |
|
| 221 | - |
|
| 222 | - if ( is_wp_error( $setting ) ) { |
|
| 223 | - return $setting; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
| 227 | - $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
| 228 | - } else { |
|
| 229 | - $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - if ( is_wp_error( $value ) ) { |
|
| 233 | - return $value; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - wpinv_update_option( $request['id'], $value ); |
|
| 237 | - $setting['value'] = $value; |
|
| 238 | - $setting = $this->sanitize_setting( $setting ); |
|
| 239 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 240 | - |
|
| 241 | - return rest_ensure_response( $response ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Makes sure the current user has access to READ the settings APIs. |
|
| 246 | - * |
|
| 247 | - * @since 2.0.0 |
|
| 248 | - * @param WP_REST_Request $request Full data about the request. |
|
| 249 | - * @return WP_Error|boolean |
|
| 250 | - */ |
|
| 251 | - public function get_items_permissions_check( $request ) { |
|
| 252 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 253 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - return true; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Makes sure the current user has access to WRITE the settings APIs. |
|
| 261 | - * |
|
| 262 | - * @since 2.0.0 |
|
| 263 | - * @param WP_REST_Request $request Full data about the request. |
|
| 264 | - * @return WP_Error|boolean |
|
| 265 | - */ |
|
| 266 | - public function update_items_permissions_check( $request ) { |
|
| 267 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 268 | - return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return true; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Check if a given request has access batch create, update and delete items. |
|
| 276 | - * |
|
| 277 | - * @param WP_REST_Request $request Full details about the request. |
|
| 278 | - * |
|
| 279 | - * @return boolean|WP_Error |
|
| 280 | - */ |
|
| 281 | - public function batch_items_permissions_check( $request ) { |
|
| 282 | - return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Prepare links for the request. |
|
| 287 | - * |
|
| 288 | - * @param string $setting_id Setting ID. |
|
| 289 | - * @return array Links for the given setting. |
|
| 290 | - */ |
|
| 291 | - protected function prepare_links( $setting_id ) { |
|
| 292 | - |
|
| 293 | - $links = array( |
|
| 294 | - 'self' => array( |
|
| 295 | - 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
| 296 | - ), |
|
| 297 | - 'collection' => array( |
|
| 298 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 299 | - ), |
|
| 300 | - ); |
|
| 301 | - |
|
| 302 | - return $links; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Prepare a settings object for serialization. |
|
| 307 | - * |
|
| 308 | - * @since 2.0.0 |
|
| 309 | - * @param array $item Setting object. |
|
| 310 | - * @param WP_REST_Request $request Request object. |
|
| 311 | - * @return WP_REST_Response $response Response data. |
|
| 312 | - */ |
|
| 313 | - public function prepare_item_for_response( $item, $request ) { |
|
| 314 | - $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
| 315 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
| 316 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 317 | - |
|
| 318 | - $response = rest_ensure_response( $data ); |
|
| 319 | - |
|
| 320 | - $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
| 321 | - |
|
| 322 | - return $response; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Filters out bad values from the settings array/filter so we |
|
| 327 | - * only return known values via the API. |
|
| 328 | - * |
|
| 329 | - * @since 2.0.0 |
|
| 330 | - * @param array $setting Setting. |
|
| 331 | - * @return array |
|
| 332 | - */ |
|
| 333 | - public function filter_setting( $setting ) { |
|
| 334 | - return array_intersect_key( |
|
| 335 | - $setting, |
|
| 336 | - array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
| 337 | - ); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Callback for allowed keys for each setting response. |
|
| 342 | - * |
|
| 343 | - * @param string $key Key to check. |
|
| 344 | - * @return boolean |
|
| 345 | - */ |
|
| 346 | - public function allowed_setting_keys( $key ) { |
|
| 347 | - return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Returns default options for a setting. null means the field is required. |
|
| 352 | - * |
|
| 353 | - * @since 2.0.0 |
|
| 354 | - * @return array |
|
| 355 | - */ |
|
| 356 | - protected function setting_defaults() { |
|
| 357 | - return array( |
|
| 358 | - 'id' => null, |
|
| 359 | - 'name' => null, |
|
| 360 | - 'desc' => '', |
|
| 361 | - 'options' => array(), |
|
| 362 | - 'std' => false, |
|
| 363 | - 'value' => false, |
|
| 364 | - 'placeholder' => '', |
|
| 365 | - 'readonly' => false, |
|
| 366 | - 'faux' => false, |
|
| 367 | - 'section' => 'main', |
|
| 368 | - 'tab' => 'general', |
|
| 369 | - 'type' => 'text', |
|
| 370 | - ); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Sanitizes a setting's field. |
|
| 375 | - * |
|
| 376 | - * @param array $setting The setting to sanitize. |
|
| 377 | - * @return array |
|
| 378 | - */ |
|
| 379 | - public function sanitize_setting( $setting ) { |
|
| 156 | + /** |
|
| 157 | + * Return all settings. |
|
| 158 | + * |
|
| 159 | + * @since 2.0.0 |
|
| 160 | + * @param WP_REST_Request $request Request data. |
|
| 161 | + * @return WP_Error|WP_REST_Response |
|
| 162 | + */ |
|
| 163 | + public function get_items( $request ) { |
|
| 164 | + |
|
| 165 | + $settings = $this->get_settings(); |
|
| 166 | + |
|
| 167 | + if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
| 168 | + return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
| 172 | + return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
| 176 | + $prepared = array(); |
|
| 177 | + |
|
| 178 | + foreach ( $settings as $setting ) { |
|
| 179 | + |
|
| 180 | + $setting = $this->sanitize_setting( $setting ); |
|
| 181 | + $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
| 182 | + $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
| 183 | + |
|
| 184 | + if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
| 185 | + $prepared[] = $setting_data; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return rest_ensure_response( $prepared ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Return a single setting. |
|
| 195 | + * |
|
| 196 | + * @since 2.0.0 |
|
| 197 | + * @param WP_REST_Request $request Request data. |
|
| 198 | + * @return WP_Error|WP_REST_Response |
|
| 199 | + */ |
|
| 200 | + public function get_item( $request ) { |
|
| 201 | + $setting = $this->get_setting( $request['id'] ); |
|
| 202 | + |
|
| 203 | + if ( is_wp_error( $setting ) ) { |
|
| 204 | + return $setting; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $setting = $this->sanitize_setting( $setting ); |
|
| 208 | + $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 209 | + return rest_ensure_response( $response ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Update a single setting. |
|
| 214 | + * |
|
| 215 | + * @since 2.0.0 |
|
| 216 | + * @param WP_REST_Request $request Request data. |
|
| 217 | + * @return WP_Error|WP_REST_Response |
|
| 218 | + */ |
|
| 219 | + public function update_item( $request ) { |
|
| 220 | + $setting = $this->get_setting( $request['id'] ); |
|
| 221 | + |
|
| 222 | + if ( is_wp_error( $setting ) ) { |
|
| 223 | + return $setting; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
| 227 | + $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
| 228 | + } else { |
|
| 229 | + $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + if ( is_wp_error( $value ) ) { |
|
| 233 | + return $value; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + wpinv_update_option( $request['id'], $value ); |
|
| 237 | + $setting['value'] = $value; |
|
| 238 | + $setting = $this->sanitize_setting( $setting ); |
|
| 239 | + $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 240 | + |
|
| 241 | + return rest_ensure_response( $response ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Makes sure the current user has access to READ the settings APIs. |
|
| 246 | + * |
|
| 247 | + * @since 2.0.0 |
|
| 248 | + * @param WP_REST_Request $request Full data about the request. |
|
| 249 | + * @return WP_Error|boolean |
|
| 250 | + */ |
|
| 251 | + public function get_items_permissions_check( $request ) { |
|
| 252 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 253 | + return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + return true; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Makes sure the current user has access to WRITE the settings APIs. |
|
| 261 | + * |
|
| 262 | + * @since 2.0.0 |
|
| 263 | + * @param WP_REST_Request $request Full data about the request. |
|
| 264 | + * @return WP_Error|boolean |
|
| 265 | + */ |
|
| 266 | + public function update_items_permissions_check( $request ) { |
|
| 267 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 268 | + return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return true; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Check if a given request has access batch create, update and delete items. |
|
| 276 | + * |
|
| 277 | + * @param WP_REST_Request $request Full details about the request. |
|
| 278 | + * |
|
| 279 | + * @return boolean|WP_Error |
|
| 280 | + */ |
|
| 281 | + public function batch_items_permissions_check( $request ) { |
|
| 282 | + return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Prepare links for the request. |
|
| 287 | + * |
|
| 288 | + * @param string $setting_id Setting ID. |
|
| 289 | + * @return array Links for the given setting. |
|
| 290 | + */ |
|
| 291 | + protected function prepare_links( $setting_id ) { |
|
| 292 | + |
|
| 293 | + $links = array( |
|
| 294 | + 'self' => array( |
|
| 295 | + 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
| 296 | + ), |
|
| 297 | + 'collection' => array( |
|
| 298 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 299 | + ), |
|
| 300 | + ); |
|
| 301 | + |
|
| 302 | + return $links; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Prepare a settings object for serialization. |
|
| 307 | + * |
|
| 308 | + * @since 2.0.0 |
|
| 309 | + * @param array $item Setting object. |
|
| 310 | + * @param WP_REST_Request $request Request object. |
|
| 311 | + * @return WP_REST_Response $response Response data. |
|
| 312 | + */ |
|
| 313 | + public function prepare_item_for_response( $item, $request ) { |
|
| 314 | + $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
| 315 | + $data = $this->add_additional_fields_to_object( $item, $request ); |
|
| 316 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 317 | + |
|
| 318 | + $response = rest_ensure_response( $data ); |
|
| 319 | + |
|
| 320 | + $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
| 321 | + |
|
| 322 | + return $response; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Filters out bad values from the settings array/filter so we |
|
| 327 | + * only return known values via the API. |
|
| 328 | + * |
|
| 329 | + * @since 2.0.0 |
|
| 330 | + * @param array $setting Setting. |
|
| 331 | + * @return array |
|
| 332 | + */ |
|
| 333 | + public function filter_setting( $setting ) { |
|
| 334 | + return array_intersect_key( |
|
| 335 | + $setting, |
|
| 336 | + array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
| 337 | + ); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Callback for allowed keys for each setting response. |
|
| 342 | + * |
|
| 343 | + * @param string $key Key to check. |
|
| 344 | + * @return boolean |
|
| 345 | + */ |
|
| 346 | + public function allowed_setting_keys( $key ) { |
|
| 347 | + return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Returns default options for a setting. null means the field is required. |
|
| 352 | + * |
|
| 353 | + * @since 2.0.0 |
|
| 354 | + * @return array |
|
| 355 | + */ |
|
| 356 | + protected function setting_defaults() { |
|
| 357 | + return array( |
|
| 358 | + 'id' => null, |
|
| 359 | + 'name' => null, |
|
| 360 | + 'desc' => '', |
|
| 361 | + 'options' => array(), |
|
| 362 | + 'std' => false, |
|
| 363 | + 'value' => false, |
|
| 364 | + 'placeholder' => '', |
|
| 365 | + 'readonly' => false, |
|
| 366 | + 'faux' => false, |
|
| 367 | + 'section' => 'main', |
|
| 368 | + 'tab' => 'general', |
|
| 369 | + 'type' => 'text', |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Sanitizes a setting's field. |
|
| 375 | + * |
|
| 376 | + * @param array $setting The setting to sanitize. |
|
| 377 | + * @return array |
|
| 378 | + */ |
|
| 379 | + public function sanitize_setting( $setting ) { |
|
| 380 | 380 | |
| 381 | - $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
| 382 | - $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
| 383 | - return $this->filter_setting( $setting ); |
|
| 384 | - |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Get setting data. |
|
| 389 | - * |
|
| 390 | - * @since 2.0.0 |
|
| 391 | - * @param string $setting_id Setting ID. |
|
| 392 | - * @return array|WP_Error |
|
| 393 | - */ |
|
| 394 | - public function get_setting( $setting_id ) { |
|
| 395 | - |
|
| 396 | - if ( empty( $setting_id ) ) { |
|
| 397 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $settings = $this->get_settings(); |
|
| 401 | - |
|
| 402 | - foreach ( $settings as $tabs ) { |
|
| 403 | - |
|
| 404 | - foreach ( $tabs as $sections ) { |
|
| 405 | - |
|
| 406 | - if ( isset( $sections[ $setting_id ] ) ) { |
|
| 407 | - if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
| 408 | - return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - return $sections[ $setting_id ]; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Get all tabs. |
|
| 423 | - * |
|
| 424 | - * @param WP_REST_Request $request Request data. |
|
| 425 | - * @return array |
|
| 426 | - */ |
|
| 427 | - public function get_tabs( $request ) { |
|
| 428 | - $tabs = wpinv_get_settings_tabs(); |
|
| 429 | - $prepared = array(); |
|
| 430 | - |
|
| 431 | - foreach ( $tabs as $id => $tab ) { |
|
| 432 | - |
|
| 433 | - $_request = $request; |
|
| 434 | - $_request['tab'] = sanitize_title( $id ); |
|
| 435 | - $data = array( |
|
| 436 | - 'id' => sanitize_title( $id ), |
|
| 437 | - 'label' => sanitize_text_field( $tab ), |
|
| 438 | - 'sections' => $this->get_sections( $_request ), |
|
| 439 | - ); |
|
| 440 | - |
|
| 441 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 442 | - $response = rest_ensure_response( $data ); |
|
| 443 | - |
|
| 444 | - if ( ! is_wp_error( $response ) ) { |
|
| 445 | - $links = array( |
|
| 446 | - 'sections' => array( |
|
| 447 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
| 448 | - ), |
|
| 449 | - 'collection' => array( |
|
| 450 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 451 | - ), |
|
| 452 | - ); |
|
| 453 | - $response->add_links( $links ); |
|
| 454 | - $response = $this->prepare_response_for_collection( $response ); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - $prepared[] = $response; |
|
| 458 | - |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - return rest_ensure_response( $prepared ); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get all sections. |
|
| 466 | - * |
|
| 467 | - * @param WP_REST_Request $request Request data. |
|
| 468 | - * @return array |
|
| 469 | - */ |
|
| 470 | - public function get_sections( $request ) { |
|
| 471 | - |
|
| 472 | - $tab = sanitize_title( $request['tab'] ); |
|
| 473 | - $sections = wpinv_get_settings_tab_sections( $tab ); |
|
| 474 | - $prepared = array(); |
|
| 475 | - |
|
| 476 | - foreach ( $sections as $id => $section ) { |
|
| 477 | - |
|
| 478 | - $data = array( |
|
| 479 | - 'id' => sanitize_title( $id ), |
|
| 480 | - 'label' => sanitize_text_field( $section ), |
|
| 481 | - ); |
|
| 482 | - |
|
| 483 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 484 | - $response = rest_ensure_response( $data ); |
|
| 485 | - |
|
| 486 | - if ( ! is_wp_error( $response ) ) { |
|
| 487 | - $links = array( |
|
| 488 | - 'settings' => array( |
|
| 489 | - 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
| 490 | - ), |
|
| 491 | - 'collection' => array( |
|
| 492 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
| 493 | - ), |
|
| 494 | - 'tabs' => array( |
|
| 495 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 496 | - ), |
|
| 497 | - ); |
|
| 498 | - $response->add_links( $links ); |
|
| 499 | - $response = $this->prepare_response_for_collection( $response ); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - $prepared[] = $response; |
|
| 503 | - |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - return rest_ensure_response( $prepared ); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Get all settings. |
|
| 511 | - * |
|
| 512 | - * @return array |
|
| 513 | - */ |
|
| 514 | - public function get_settings() { |
|
| 515 | - |
|
| 516 | - if ( empty( $this->settings ) ) { |
|
| 517 | - $this->settings = wpinv_get_registered_settings(); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - return $this->settings; |
|
| 521 | - |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Boolean for if a setting type is a valid supported setting type. |
|
| 526 | - * |
|
| 527 | - * @since 2.0.0 |
|
| 528 | - * @param string $type Type. |
|
| 529 | - * @return bool |
|
| 530 | - */ |
|
| 531 | - public function is_setting_type_valid( $type ) { |
|
| 532 | - |
|
| 533 | - return in_array( |
|
| 534 | - $type, array( |
|
| 535 | - 'text', // Validates with validate_setting_text_field. |
|
| 536 | - 'email', // Validates with validate_setting_text_field. |
|
| 537 | - 'number', // Validates with validate_setting_text_field. |
|
| 538 | - 'color', // Validates with validate_setting_text_field. |
|
| 539 | - 'password', // Validates with validate_setting_text_field. |
|
| 540 | - 'textarea', // Validates with validate_setting_textarea_field. |
|
| 541 | - 'select', // Validates with validate_setting_select_field. |
|
| 542 | - 'multiselect', // Validates with validate_setting_multiselect_field. |
|
| 543 | - 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
| 544 | - 'checkbox', // Validates with validate_setting_checkbox_field. |
|
| 545 | - 'header', // Validates with validate_setting_text_field. |
|
| 546 | - ) |
|
| 547 | - ); |
|
| 548 | - |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Get the settings schema, conforming to JSON Schema. |
|
| 553 | - * |
|
| 554 | - * @return array |
|
| 555 | - */ |
|
| 556 | - public function get_item_schema() { |
|
| 557 | - |
|
| 558 | - // Maybe retrieve the schema from cache. |
|
| 559 | - if ( ! empty( $this->schema ) ) { |
|
| 560 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - $schema = array( |
|
| 564 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 565 | - 'title' => 'setting', |
|
| 566 | - 'type' => 'object', |
|
| 567 | - 'properties' => array( |
|
| 568 | - 'id' => array( |
|
| 569 | - 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
| 570 | - 'type' => 'string', |
|
| 571 | - 'arg_options' => array( |
|
| 572 | - 'sanitize_callback' => 'sanitize_title', |
|
| 573 | - ), |
|
| 574 | - 'context' => array( 'view', 'edit' ), |
|
| 575 | - 'readonly' => true, |
|
| 576 | - ), |
|
| 577 | - 'tab' => array( |
|
| 578 | - 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
| 579 | - 'type' => 'string', |
|
| 580 | - 'arg_options' => array( |
|
| 581 | - 'sanitize_callback' => 'sanitize_title', |
|
| 582 | - ), |
|
| 583 | - 'context' => array( 'view', 'edit' ), |
|
| 584 | - 'readonly' => true, |
|
| 585 | - ), |
|
| 586 | - 'section' => array( |
|
| 587 | - 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
| 588 | - 'type' => 'string', |
|
| 589 | - 'arg_options' => array( |
|
| 590 | - 'sanitize_callback' => 'sanitize_title', |
|
| 591 | - ), |
|
| 592 | - 'context' => array( 'view', 'edit' ), |
|
| 593 | - 'readonly' => true, |
|
| 594 | - ), |
|
| 595 | - 'name' => array( |
|
| 596 | - 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
| 597 | - 'type' => 'string', |
|
| 598 | - 'arg_options' => array( |
|
| 599 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 600 | - ), |
|
| 601 | - 'context' => array( 'view', 'edit' ), |
|
| 602 | - 'readonly' => true, |
|
| 603 | - ), |
|
| 604 | - 'desc' => array( |
|
| 605 | - 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
| 606 | - 'type' => 'string', |
|
| 607 | - 'context' => array( 'view', 'edit' ), |
|
| 608 | - 'readonly' => true, |
|
| 609 | - ), |
|
| 610 | - 'value' => array( |
|
| 611 | - 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
| 612 | - 'type' => 'mixed', |
|
| 613 | - 'context' => array( 'view', 'edit' ), |
|
| 614 | - ), |
|
| 615 | - 'default' => array( |
|
| 616 | - 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
| 617 | - 'type' => 'mixed', |
|
| 618 | - 'context' => array( 'view', 'edit' ), |
|
| 619 | - 'readonly' => true, |
|
| 620 | - ), |
|
| 621 | - 'placeholder' => array( |
|
| 622 | - 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
| 623 | - 'type' => 'string', |
|
| 624 | - 'arg_options' => array( |
|
| 625 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 626 | - ), |
|
| 627 | - 'context' => array( 'view', 'edit' ), |
|
| 628 | - 'readonly' => true, |
|
| 629 | - ), |
|
| 630 | - 'type' => array( |
|
| 631 | - 'description' => __( 'Type of setting.', 'invoicing' ), |
|
| 632 | - 'type' => 'string', |
|
| 633 | - 'arg_options' => array( |
|
| 634 | - 'sanitize_callback' => 'sanitize_text_field', |
|
| 635 | - ), |
|
| 636 | - 'context' => array( 'view', 'edit' ), |
|
| 637 | - 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
| 638 | - 'readonly' => true, |
|
| 639 | - ), |
|
| 640 | - 'options' => array( |
|
| 641 | - 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
| 642 | - 'type' => 'object', |
|
| 643 | - 'context' => array( 'view', 'edit' ), |
|
| 644 | - 'readonly' => true, |
|
| 645 | - ), |
|
| 646 | - 'readonly' => array( |
|
| 647 | - 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
| 648 | - 'type' => 'string', |
|
| 649 | - 'context' => array( 'view' ), |
|
| 650 | - 'readonly' => true, |
|
| 651 | - ), |
|
| 652 | - 'faux' => array( |
|
| 653 | - 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
| 654 | - 'type' => 'string', |
|
| 655 | - 'context' => array( 'view' ), |
|
| 656 | - 'readonly' => true, |
|
| 657 | - ), |
|
| 658 | - ), |
|
| 659 | - ); |
|
| 660 | - |
|
| 661 | - // Filters the settings schema for the REST API. |
|
| 381 | + $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
| 382 | + $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
| 383 | + return $this->filter_setting( $setting ); |
|
| 384 | + |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Get setting data. |
|
| 389 | + * |
|
| 390 | + * @since 2.0.0 |
|
| 391 | + * @param string $setting_id Setting ID. |
|
| 392 | + * @return array|WP_Error |
|
| 393 | + */ |
|
| 394 | + public function get_setting( $setting_id ) { |
|
| 395 | + |
|
| 396 | + if ( empty( $setting_id ) ) { |
|
| 397 | + return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $settings = $this->get_settings(); |
|
| 401 | + |
|
| 402 | + foreach ( $settings as $tabs ) { |
|
| 403 | + |
|
| 404 | + foreach ( $tabs as $sections ) { |
|
| 405 | + |
|
| 406 | + if ( isset( $sections[ $setting_id ] ) ) { |
|
| 407 | + if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
| 408 | + return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + return $sections[ $setting_id ]; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Get all tabs. |
|
| 423 | + * |
|
| 424 | + * @param WP_REST_Request $request Request data. |
|
| 425 | + * @return array |
|
| 426 | + */ |
|
| 427 | + public function get_tabs( $request ) { |
|
| 428 | + $tabs = wpinv_get_settings_tabs(); |
|
| 429 | + $prepared = array(); |
|
| 430 | + |
|
| 431 | + foreach ( $tabs as $id => $tab ) { |
|
| 432 | + |
|
| 433 | + $_request = $request; |
|
| 434 | + $_request['tab'] = sanitize_title( $id ); |
|
| 435 | + $data = array( |
|
| 436 | + 'id' => sanitize_title( $id ), |
|
| 437 | + 'label' => sanitize_text_field( $tab ), |
|
| 438 | + 'sections' => $this->get_sections( $_request ), |
|
| 439 | + ); |
|
| 440 | + |
|
| 441 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 442 | + $response = rest_ensure_response( $data ); |
|
| 443 | + |
|
| 444 | + if ( ! is_wp_error( $response ) ) { |
|
| 445 | + $links = array( |
|
| 446 | + 'sections' => array( |
|
| 447 | + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
| 448 | + ), |
|
| 449 | + 'collection' => array( |
|
| 450 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 451 | + ), |
|
| 452 | + ); |
|
| 453 | + $response->add_links( $links ); |
|
| 454 | + $response = $this->prepare_response_for_collection( $response ); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + $prepared[] = $response; |
|
| 458 | + |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + return rest_ensure_response( $prepared ); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get all sections. |
|
| 466 | + * |
|
| 467 | + * @param WP_REST_Request $request Request data. |
|
| 468 | + * @return array |
|
| 469 | + */ |
|
| 470 | + public function get_sections( $request ) { |
|
| 471 | + |
|
| 472 | + $tab = sanitize_title( $request['tab'] ); |
|
| 473 | + $sections = wpinv_get_settings_tab_sections( $tab ); |
|
| 474 | + $prepared = array(); |
|
| 475 | + |
|
| 476 | + foreach ( $sections as $id => $section ) { |
|
| 477 | + |
|
| 478 | + $data = array( |
|
| 479 | + 'id' => sanitize_title( $id ), |
|
| 480 | + 'label' => sanitize_text_field( $section ), |
|
| 481 | + ); |
|
| 482 | + |
|
| 483 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 484 | + $response = rest_ensure_response( $data ); |
|
| 485 | + |
|
| 486 | + if ( ! is_wp_error( $response ) ) { |
|
| 487 | + $links = array( |
|
| 488 | + 'settings' => array( |
|
| 489 | + 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
| 490 | + ), |
|
| 491 | + 'collection' => array( |
|
| 492 | + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
| 493 | + ), |
|
| 494 | + 'tabs' => array( |
|
| 495 | + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 496 | + ), |
|
| 497 | + ); |
|
| 498 | + $response->add_links( $links ); |
|
| 499 | + $response = $this->prepare_response_for_collection( $response ); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + $prepared[] = $response; |
|
| 503 | + |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + return rest_ensure_response( $prepared ); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Get all settings. |
|
| 511 | + * |
|
| 512 | + * @return array |
|
| 513 | + */ |
|
| 514 | + public function get_settings() { |
|
| 515 | + |
|
| 516 | + if ( empty( $this->settings ) ) { |
|
| 517 | + $this->settings = wpinv_get_registered_settings(); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + return $this->settings; |
|
| 521 | + |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Boolean for if a setting type is a valid supported setting type. |
|
| 526 | + * |
|
| 527 | + * @since 2.0.0 |
|
| 528 | + * @param string $type Type. |
|
| 529 | + * @return bool |
|
| 530 | + */ |
|
| 531 | + public function is_setting_type_valid( $type ) { |
|
| 532 | + |
|
| 533 | + return in_array( |
|
| 534 | + $type, array( |
|
| 535 | + 'text', // Validates with validate_setting_text_field. |
|
| 536 | + 'email', // Validates with validate_setting_text_field. |
|
| 537 | + 'number', // Validates with validate_setting_text_field. |
|
| 538 | + 'color', // Validates with validate_setting_text_field. |
|
| 539 | + 'password', // Validates with validate_setting_text_field. |
|
| 540 | + 'textarea', // Validates with validate_setting_textarea_field. |
|
| 541 | + 'select', // Validates with validate_setting_select_field. |
|
| 542 | + 'multiselect', // Validates with validate_setting_multiselect_field. |
|
| 543 | + 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
| 544 | + 'checkbox', // Validates with validate_setting_checkbox_field. |
|
| 545 | + 'header', // Validates with validate_setting_text_field. |
|
| 546 | + ) |
|
| 547 | + ); |
|
| 548 | + |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Get the settings schema, conforming to JSON Schema. |
|
| 553 | + * |
|
| 554 | + * @return array |
|
| 555 | + */ |
|
| 556 | + public function get_item_schema() { |
|
| 557 | + |
|
| 558 | + // Maybe retrieve the schema from cache. |
|
| 559 | + if ( ! empty( $this->schema ) ) { |
|
| 560 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + $schema = array( |
|
| 564 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 565 | + 'title' => 'setting', |
|
| 566 | + 'type' => 'object', |
|
| 567 | + 'properties' => array( |
|
| 568 | + 'id' => array( |
|
| 569 | + 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
| 570 | + 'type' => 'string', |
|
| 571 | + 'arg_options' => array( |
|
| 572 | + 'sanitize_callback' => 'sanitize_title', |
|
| 573 | + ), |
|
| 574 | + 'context' => array( 'view', 'edit' ), |
|
| 575 | + 'readonly' => true, |
|
| 576 | + ), |
|
| 577 | + 'tab' => array( |
|
| 578 | + 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
| 579 | + 'type' => 'string', |
|
| 580 | + 'arg_options' => array( |
|
| 581 | + 'sanitize_callback' => 'sanitize_title', |
|
| 582 | + ), |
|
| 583 | + 'context' => array( 'view', 'edit' ), |
|
| 584 | + 'readonly' => true, |
|
| 585 | + ), |
|
| 586 | + 'section' => array( |
|
| 587 | + 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
| 588 | + 'type' => 'string', |
|
| 589 | + 'arg_options' => array( |
|
| 590 | + 'sanitize_callback' => 'sanitize_title', |
|
| 591 | + ), |
|
| 592 | + 'context' => array( 'view', 'edit' ), |
|
| 593 | + 'readonly' => true, |
|
| 594 | + ), |
|
| 595 | + 'name' => array( |
|
| 596 | + 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
| 597 | + 'type' => 'string', |
|
| 598 | + 'arg_options' => array( |
|
| 599 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 600 | + ), |
|
| 601 | + 'context' => array( 'view', 'edit' ), |
|
| 602 | + 'readonly' => true, |
|
| 603 | + ), |
|
| 604 | + 'desc' => array( |
|
| 605 | + 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
| 606 | + 'type' => 'string', |
|
| 607 | + 'context' => array( 'view', 'edit' ), |
|
| 608 | + 'readonly' => true, |
|
| 609 | + ), |
|
| 610 | + 'value' => array( |
|
| 611 | + 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
| 612 | + 'type' => 'mixed', |
|
| 613 | + 'context' => array( 'view', 'edit' ), |
|
| 614 | + ), |
|
| 615 | + 'default' => array( |
|
| 616 | + 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
| 617 | + 'type' => 'mixed', |
|
| 618 | + 'context' => array( 'view', 'edit' ), |
|
| 619 | + 'readonly' => true, |
|
| 620 | + ), |
|
| 621 | + 'placeholder' => array( |
|
| 622 | + 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
| 623 | + 'type' => 'string', |
|
| 624 | + 'arg_options' => array( |
|
| 625 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 626 | + ), |
|
| 627 | + 'context' => array( 'view', 'edit' ), |
|
| 628 | + 'readonly' => true, |
|
| 629 | + ), |
|
| 630 | + 'type' => array( |
|
| 631 | + 'description' => __( 'Type of setting.', 'invoicing' ), |
|
| 632 | + 'type' => 'string', |
|
| 633 | + 'arg_options' => array( |
|
| 634 | + 'sanitize_callback' => 'sanitize_text_field', |
|
| 635 | + ), |
|
| 636 | + 'context' => array( 'view', 'edit' ), |
|
| 637 | + 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
| 638 | + 'readonly' => true, |
|
| 639 | + ), |
|
| 640 | + 'options' => array( |
|
| 641 | + 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
| 642 | + 'type' => 'object', |
|
| 643 | + 'context' => array( 'view', 'edit' ), |
|
| 644 | + 'readonly' => true, |
|
| 645 | + ), |
|
| 646 | + 'readonly' => array( |
|
| 647 | + 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
| 648 | + 'type' => 'string', |
|
| 649 | + 'context' => array( 'view' ), |
|
| 650 | + 'readonly' => true, |
|
| 651 | + ), |
|
| 652 | + 'faux' => array( |
|
| 653 | + 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
| 654 | + 'type' => 'string', |
|
| 655 | + 'context' => array( 'view' ), |
|
| 656 | + 'readonly' => true, |
|
| 657 | + ), |
|
| 658 | + ), |
|
| 659 | + ); |
|
| 660 | + |
|
| 661 | + // Filters the settings schema for the REST API. |
|
| 662 | 662 | $schema = apply_filters( 'getpaid_rest_settings_schema', $schema ); |
| 663 | 663 | |
| 664 | - // Cache the settings schema. |
|
| 665 | - $this->schema = $schema; |
|
| 666 | - |
|
| 667 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 668 | - |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - /** |
|
| 672 | - * Validate a text value for a text based setting. |
|
| 673 | - * |
|
| 674 | - * @since 2.0.0 |
|
| 675 | - * @param string $value Value. |
|
| 676 | - * @param array $setting Setting. |
|
| 677 | - * @return string |
|
| 678 | - */ |
|
| 679 | - public function validate_setting_text_field( $value ) { |
|
| 680 | - $value = is_null( $value ) ? '' : $value; |
|
| 681 | - return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Validate select based settings. |
|
| 686 | - * |
|
| 687 | - * @since 2.0.0 |
|
| 688 | - * @param string $value Value. |
|
| 689 | - * @param array $setting Setting. |
|
| 690 | - * @return string|WP_Error |
|
| 691 | - */ |
|
| 692 | - public function validate_setting_select_field( $value, $setting ) { |
|
| 693 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 694 | - return $value; |
|
| 695 | - } else { |
|
| 696 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Validate multiselect based settings. |
|
| 702 | - * |
|
| 703 | - * @since 2.0.0 |
|
| 704 | - * @param array $values Values. |
|
| 705 | - * @param array $setting Setting. |
|
| 706 | - * @return array|WP_Error |
|
| 707 | - */ |
|
| 708 | - public function validate_setting_multiselect_field( $values, $setting ) { |
|
| 709 | - if ( empty( $values ) ) { |
|
| 710 | - return array(); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - if ( ! is_array( $values ) ) { |
|
| 714 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - $final_values = array(); |
|
| 718 | - foreach ( $values as $value ) { |
|
| 719 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 720 | - $final_values[] = $value; |
|
| 721 | - } |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - return $final_values; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Validate radio based settings. |
|
| 729 | - * |
|
| 730 | - * @since 2.0.0 |
|
| 731 | - * @param string $value Value. |
|
| 732 | - * @param array $setting Setting. |
|
| 733 | - * @return string|WP_Error |
|
| 734 | - */ |
|
| 735 | - public function validate_setting_radio_field( $value, $setting ) { |
|
| 736 | - return $this->validate_setting_select_field( $value, $setting ); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Validate checkbox based settings. |
|
| 741 | - * |
|
| 742 | - * @since 2.0.0 |
|
| 743 | - * @param string $value Value. |
|
| 744 | - * @return int |
|
| 745 | - */ |
|
| 746 | - public function validate_setting_checkbox_field( $value ) { |
|
| 747 | - return (int) ! empty( $value ); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Validate textarea based settings. |
|
| 752 | - * |
|
| 753 | - * @since 2.0.0 |
|
| 754 | - * @param string $value Value. |
|
| 755 | - * @return string |
|
| 756 | - */ |
|
| 757 | - public function validate_setting_textarea_field( $value ) { |
|
| 758 | - $value = is_null( $value ) ? '' : $value; |
|
| 759 | - return wp_kses( |
|
| 760 | - trim( stripslashes( $value ) ), |
|
| 761 | - array_merge( |
|
| 762 | - array( |
|
| 763 | - 'iframe' => array( |
|
| 764 | - 'src' => true, |
|
| 765 | - 'style' => true, |
|
| 766 | - 'id' => true, |
|
| 767 | - 'class' => true, |
|
| 768 | - ), |
|
| 769 | - ), |
|
| 770 | - wp_kses_allowed_html( 'post' ) |
|
| 771 | - ) |
|
| 772 | - ); |
|
| 773 | - } |
|
| 664 | + // Cache the settings schema. |
|
| 665 | + $this->schema = $schema; |
|
| 666 | + |
|
| 667 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 668 | + |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + /** |
|
| 672 | + * Validate a text value for a text based setting. |
|
| 673 | + * |
|
| 674 | + * @since 2.0.0 |
|
| 675 | + * @param string $value Value. |
|
| 676 | + * @param array $setting Setting. |
|
| 677 | + * @return string |
|
| 678 | + */ |
|
| 679 | + public function validate_setting_text_field( $value ) { |
|
| 680 | + $value = is_null( $value ) ? '' : $value; |
|
| 681 | + return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Validate select based settings. |
|
| 686 | + * |
|
| 687 | + * @since 2.0.0 |
|
| 688 | + * @param string $value Value. |
|
| 689 | + * @param array $setting Setting. |
|
| 690 | + * @return string|WP_Error |
|
| 691 | + */ |
|
| 692 | + public function validate_setting_select_field( $value, $setting ) { |
|
| 693 | + if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 694 | + return $value; |
|
| 695 | + } else { |
|
| 696 | + return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Validate multiselect based settings. |
|
| 702 | + * |
|
| 703 | + * @since 2.0.0 |
|
| 704 | + * @param array $values Values. |
|
| 705 | + * @param array $setting Setting. |
|
| 706 | + * @return array|WP_Error |
|
| 707 | + */ |
|
| 708 | + public function validate_setting_multiselect_field( $values, $setting ) { |
|
| 709 | + if ( empty( $values ) ) { |
|
| 710 | + return array(); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + if ( ! is_array( $values ) ) { |
|
| 714 | + return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + $final_values = array(); |
|
| 718 | + foreach ( $values as $value ) { |
|
| 719 | + if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 720 | + $final_values[] = $value; |
|
| 721 | + } |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + return $final_values; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Validate radio based settings. |
|
| 729 | + * |
|
| 730 | + * @since 2.0.0 |
|
| 731 | + * @param string $value Value. |
|
| 732 | + * @param array $setting Setting. |
|
| 733 | + * @return string|WP_Error |
|
| 734 | + */ |
|
| 735 | + public function validate_setting_radio_field( $value, $setting ) { |
|
| 736 | + return $this->validate_setting_select_field( $value, $setting ); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Validate checkbox based settings. |
|
| 741 | + * |
|
| 742 | + * @since 2.0.0 |
|
| 743 | + * @param string $value Value. |
|
| 744 | + * @return int |
|
| 745 | + */ |
|
| 746 | + public function validate_setting_checkbox_field( $value ) { |
|
| 747 | + return (int) ! empty( $value ); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Validate textarea based settings. |
|
| 752 | + * |
|
| 753 | + * @since 2.0.0 |
|
| 754 | + * @param string $value Value. |
|
| 755 | + * @return string |
|
| 756 | + */ |
|
| 757 | + public function validate_setting_textarea_field( $value ) { |
|
| 758 | + $value = is_null( $value ) ? '' : $value; |
|
| 759 | + return wp_kses( |
|
| 760 | + trim( stripslashes( $value ) ), |
|
| 761 | + array_merge( |
|
| 762 | + array( |
|
| 763 | + 'iframe' => array( |
|
| 764 | + 'src' => true, |
|
| 765 | + 'style' => true, |
|
| 766 | + 'id' => true, |
|
| 767 | + 'class' => true, |
|
| 768 | + ), |
|
| 769 | + ), |
|
| 770 | + wp_kses_allowed_html( 'post' ) |
|
| 771 | + ) |
|
| 772 | + ); |
|
| 773 | + } |
|
| 774 | 774 | |
| 775 | 775 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | * @since 2.0.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | -defined( 'ABSPATH' ) || exit; |
|
| 12 | +defined('ABSPATH') || exit; |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * GetPaid REST Setting controller class. |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @see register_rest_route() |
| 41 | 41 | */ |
| 42 | - public function register_namespace_routes( $namespace ) { |
|
| 42 | + public function register_namespace_routes($namespace) { |
|
| 43 | 43 | |
| 44 | 44 | // List all registered tabs. |
| 45 | 45 | register_rest_route( |
@@ -48,8 +48,8 @@ discard block |
||
| 48 | 48 | array( |
| 49 | 49 | array( |
| 50 | 50 | 'methods' => WP_REST_Server::READABLE, |
| 51 | - 'callback' => array( $this, 'get_tabs' ), |
|
| 52 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 51 | + 'callback' => array($this, 'get_tabs'), |
|
| 52 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
| 53 | 53 | ), |
| 54 | 54 | 'schema' => '__return_empty_array', |
| 55 | 55 | ) |
@@ -62,23 +62,23 @@ discard block |
||
| 62 | 62 | array( |
| 63 | 63 | 'args' => array( |
| 64 | 64 | 'id' => array( |
| 65 | - 'description' => __( 'Unique identifier for the setting.', 'invoicing' ), |
|
| 65 | + 'description' => __('Unique identifier for the setting.', 'invoicing'), |
|
| 66 | 66 | 'type' => 'string', |
| 67 | 67 | 'required' => true, |
| 68 | 68 | ), |
| 69 | 69 | ), |
| 70 | 70 | array( |
| 71 | 71 | 'methods' => WP_REST_Server::READABLE, |
| 72 | - 'callback' => array( $this, 'get_item' ), |
|
| 73 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 72 | + 'callback' => array($this, 'get_item'), |
|
| 73 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
| 74 | 74 | ), |
| 75 | 75 | array( |
| 76 | 76 | 'methods' => WP_REST_Server::EDITABLE, |
| 77 | - 'callback' => array( $this, 'update_item' ), |
|
| 78 | - 'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
| 79 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 77 | + 'callback' => array($this, 'update_item'), |
|
| 78 | + 'permission_callback' => array($this, 'update_items_permissions_check'), |
|
| 79 | + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), |
|
| 80 | 80 | ), |
| 81 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 81 | + 'schema' => array($this, 'get_public_item_schema'), |
|
| 82 | 82 | ) |
| 83 | 83 | ); |
| 84 | 84 | |
@@ -89,16 +89,16 @@ discard block |
||
| 89 | 89 | array( |
| 90 | 90 | 'args' => array( |
| 91 | 91 | 'tab' => array( |
| 92 | - 'description' => __( 'Unique identifier for the tab whose sections should be retrieved.', 'invoicing' ), |
|
| 92 | + 'description' => __('Unique identifier for the tab whose sections should be retrieved.', 'invoicing'), |
|
| 93 | 93 | 'type' => 'string', |
| 94 | 94 | 'required' => true, |
| 95 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 95 | + 'enum' => array_keys(wpinv_get_settings_tabs()), |
|
| 96 | 96 | ), |
| 97 | 97 | ), |
| 98 | 98 | array( |
| 99 | 99 | 'methods' => WP_REST_Server::READABLE, |
| 100 | - 'callback' => array( $this, 'get_sections' ), |
|
| 101 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 100 | + 'callback' => array($this, 'get_sections'), |
|
| 101 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
| 102 | 102 | ), |
| 103 | 103 | 'schema' => '__return_empty_array', |
| 104 | 104 | ) |
@@ -111,23 +111,23 @@ discard block |
||
| 111 | 111 | array( |
| 112 | 112 | 'args' => array( |
| 113 | 113 | 'tab' => array( |
| 114 | - 'description' => __( 'Unique identifier for the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 114 | + 'description' => __('Unique identifier for the tab whose settings should be retrieved.', 'invoicing'), |
|
| 115 | 115 | 'type' => 'string', |
| 116 | 116 | 'required' => true, |
| 117 | - 'enum' => array_keys( wpinv_get_settings_tabs() ), |
|
| 117 | + 'enum' => array_keys(wpinv_get_settings_tabs()), |
|
| 118 | 118 | ), |
| 119 | 119 | 'section' => array( |
| 120 | - 'description' => __( 'The section in the tab whose settings should be retrieved.', 'invoicing' ), |
|
| 120 | + 'description' => __('The section in the tab whose settings should be retrieved.', 'invoicing'), |
|
| 121 | 121 | 'type' => 'string', |
| 122 | 122 | 'required' => true, |
| 123 | 123 | ), |
| 124 | 124 | ), |
| 125 | 125 | array( |
| 126 | 126 | 'methods' => WP_REST_Server::READABLE, |
| 127 | - 'callback' => array( $this, 'get_items' ), |
|
| 128 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
| 127 | + 'callback' => array($this, 'get_items'), |
|
| 128 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
| 129 | 129 | ), |
| 130 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
| 130 | + 'schema' => array($this, 'get_public_item_schema'), |
|
| 131 | 131 | ) |
| 132 | 132 | ); |
| 133 | 133 | |
@@ -137,17 +137,17 @@ discard block |
||
| 137 | 137 | array( |
| 138 | 138 | 'args' => array( |
| 139 | 139 | 'id' => array( |
| 140 | - 'description' => __( 'Setting ID.', 'invoicing' ), |
|
| 140 | + 'description' => __('Setting ID.', 'invoicing'), |
|
| 141 | 141 | 'type' => 'string', |
| 142 | 142 | ), |
| 143 | 143 | ), |
| 144 | 144 | array( |
| 145 | 145 | 'methods' => WP_REST_Server::EDITABLE, |
| 146 | - 'callback' => array( $this, 'batch_items' ), |
|
| 147 | - 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
| 148 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
| 146 | + 'callback' => array($this, 'batch_items'), |
|
| 147 | + 'permission_callback' => array($this, 'batch_items_permissions_check'), |
|
| 148 | + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), |
|
| 149 | 149 | ), |
| 150 | - 'schema' => array( $this, 'get_public_batch_schema' ), |
|
| 150 | + 'schema' => array($this, 'get_public_batch_schema'), |
|
| 151 | 151 | ) |
| 152 | 152 | ); |
| 153 | 153 | |
@@ -160,34 +160,34 @@ discard block |
||
| 160 | 160 | * @param WP_REST_Request $request Request data. |
| 161 | 161 | * @return WP_Error|WP_REST_Response |
| 162 | 162 | */ |
| 163 | - public function get_items( $request ) { |
|
| 163 | + public function get_items($request) { |
|
| 164 | 164 | |
| 165 | 165 | $settings = $this->get_settings(); |
| 166 | 166 | |
| 167 | - if ( ! isset( $settings[ $request['tab'] ] ) ) { |
|
| 168 | - return new WP_Error( 'rest_invalid_tab', __( 'Invalid tab.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 167 | + if (!isset($settings[$request['tab']])) { |
|
| 168 | + return new WP_Error('rest_invalid_tab', __('Invalid tab.', 'invoicing'), array('status' => 400)); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - if ( ! isset( $settings[ $request['tab'] ][ $request['section'] ] ) ) { |
|
| 172 | - return new WP_Error( 'rest_invalid_section', __( 'Invalid section.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 171 | + if (!isset($settings[$request['tab']][$request['section']])) { |
|
| 172 | + return new WP_Error('rest_invalid_section', __('Invalid section.', 'invoicing'), array('status' => 400)); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - $settings = $settings[ $request['tab'] ][ $request['section'] ]; |
|
| 175 | + $settings = $settings[$request['tab']][$request['section']]; |
|
| 176 | 176 | $prepared = array(); |
| 177 | 177 | |
| 178 | - foreach ( $settings as $setting ) { |
|
| 178 | + foreach ($settings as $setting) { |
|
| 179 | 179 | |
| 180 | - $setting = $this->sanitize_setting( $setting ); |
|
| 181 | - $setting_data = $this->prepare_item_for_response( $setting, $request ); |
|
| 182 | - $setting_data = $this->prepare_response_for_collection( $setting_data ); |
|
| 180 | + $setting = $this->sanitize_setting($setting); |
|
| 181 | + $setting_data = $this->prepare_item_for_response($setting, $request); |
|
| 182 | + $setting_data = $this->prepare_response_for_collection($setting_data); |
|
| 183 | 183 | |
| 184 | - if ( $this->is_setting_type_valid( $setting['type'] ) ) { |
|
| 185 | - $prepared[] = $setting_data; |
|
| 184 | + if ($this->is_setting_type_valid($setting['type'])) { |
|
| 185 | + $prepared[] = $setting_data; |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - return rest_ensure_response( $prepared ); |
|
| 190 | + return rest_ensure_response($prepared); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /** |
@@ -197,16 +197,16 @@ discard block |
||
| 197 | 197 | * @param WP_REST_Request $request Request data. |
| 198 | 198 | * @return WP_Error|WP_REST_Response |
| 199 | 199 | */ |
| 200 | - public function get_item( $request ) { |
|
| 201 | - $setting = $this->get_setting( $request['id'] ); |
|
| 200 | + public function get_item($request) { |
|
| 201 | + $setting = $this->get_setting($request['id']); |
|
| 202 | 202 | |
| 203 | - if ( is_wp_error( $setting ) ) { |
|
| 203 | + if (is_wp_error($setting)) { |
|
| 204 | 204 | return $setting; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - $setting = $this->sanitize_setting( $setting ); |
|
| 208 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 209 | - return rest_ensure_response( $response ); |
|
| 207 | + $setting = $this->sanitize_setting($setting); |
|
| 208 | + $response = $this->prepare_item_for_response($setting, $request); |
|
| 209 | + return rest_ensure_response($response); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -216,29 +216,29 @@ discard block |
||
| 216 | 216 | * @param WP_REST_Request $request Request data. |
| 217 | 217 | * @return WP_Error|WP_REST_Response |
| 218 | 218 | */ |
| 219 | - public function update_item( $request ) { |
|
| 220 | - $setting = $this->get_setting( $request['id'] ); |
|
| 219 | + public function update_item($request) { |
|
| 220 | + $setting = $this->get_setting($request['id']); |
|
| 221 | 221 | |
| 222 | - if ( is_wp_error( $setting ) ) { |
|
| 222 | + if (is_wp_error($setting)) { |
|
| 223 | 223 | return $setting; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - if ( is_callable( array( $this, 'validate_setting_' . $setting['type'] . '_field' ) ) ) { |
|
| 227 | - $value = $this->{'validate_setting_' . $setting['type'] . '_field'}( $request['value'], $setting ); |
|
| 226 | + if (is_callable(array($this, 'validate_setting_' . $setting['type'] . '_field'))) { |
|
| 227 | + $value = $this->{'validate_setting_' . $setting['type'] . '_field'}($request['value'], $setting); |
|
| 228 | 228 | } else { |
| 229 | - $value = $this->validate_setting_text_field( $request['value'], $setting ); |
|
| 229 | + $value = $this->validate_setting_text_field($request['value'], $setting); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - if ( is_wp_error( $value ) ) { |
|
| 232 | + if (is_wp_error($value)) { |
|
| 233 | 233 | return $value; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - wpinv_update_option( $request['id'], $value ); |
|
| 236 | + wpinv_update_option($request['id'], $value); |
|
| 237 | 237 | $setting['value'] = $value; |
| 238 | - $setting = $this->sanitize_setting( $setting ); |
|
| 239 | - $response = $this->prepare_item_for_response( $setting, $request ); |
|
| 238 | + $setting = $this->sanitize_setting($setting); |
|
| 239 | + $response = $this->prepare_item_for_response($setting, $request); |
|
| 240 | 240 | |
| 241 | - return rest_ensure_response( $response ); |
|
| 241 | + return rest_ensure_response($response); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -248,9 +248,9 @@ discard block |
||
| 248 | 248 | * @param WP_REST_Request $request Full data about the request. |
| 249 | 249 | * @return WP_Error|boolean |
| 250 | 250 | */ |
| 251 | - public function get_items_permissions_check( $request ) { |
|
| 252 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 253 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 251 | + public function get_items_permissions_check($request) { |
|
| 252 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 253 | + return new WP_Error('rest_cannot_view', __('Sorry, you cannot list resources.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | return true; |
@@ -263,9 +263,9 @@ discard block |
||
| 263 | 263 | * @param WP_REST_Request $request Full data about the request. |
| 264 | 264 | * @return WP_Error|boolean |
| 265 | 265 | */ |
| 266 | - public function update_items_permissions_check( $request ) { |
|
| 267 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 268 | - return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 266 | + public function update_items_permissions_check($request) { |
|
| 267 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 268 | + return new WP_Error('rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | return true; |
@@ -278,8 +278,8 @@ discard block |
||
| 278 | 278 | * |
| 279 | 279 | * @return boolean|WP_Error |
| 280 | 280 | */ |
| 281 | - public function batch_items_permissions_check( $request ) { |
|
| 282 | - return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 281 | + public function batch_items_permissions_check($request) { |
|
| 282 | + return wpinv_current_user_can_manage_invoicing() ? true : new WP_Error('rest_cannot_batch', __('Sorry, you are not allowed to batch manipulate this resource.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
@@ -288,14 +288,14 @@ discard block |
||
| 288 | 288 | * @param string $setting_id Setting ID. |
| 289 | 289 | * @return array Links for the given setting. |
| 290 | 290 | */ |
| 291 | - protected function prepare_links( $setting_id ) { |
|
| 291 | + protected function prepare_links($setting_id) { |
|
| 292 | 292 | |
| 293 | 293 | $links = array( |
| 294 | 294 | 'self' => array( |
| 295 | - 'href' => rest_url( sprintf( '/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id ) ), |
|
| 295 | + 'href' => rest_url(sprintf('/%s/%s/setting/%s', $this->namespace, $this->rest_base, $setting_id)), |
|
| 296 | 296 | ), |
| 297 | 297 | 'collection' => array( |
| 298 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 298 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
| 299 | 299 | ), |
| 300 | 300 | ); |
| 301 | 301 | |
@@ -310,14 +310,14 @@ discard block |
||
| 310 | 310 | * @param WP_REST_Request $request Request object. |
| 311 | 311 | * @return WP_REST_Response $response Response data. |
| 312 | 312 | */ |
| 313 | - public function prepare_item_for_response( $item, $request ) { |
|
| 314 | - $context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
| 315 | - $data = $this->add_additional_fields_to_object( $item, $request ); |
|
| 316 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 313 | + public function prepare_item_for_response($item, $request) { |
|
| 314 | + $context = empty($request['context']) ? 'view' : $request['context']; |
|
| 315 | + $data = $this->add_additional_fields_to_object($item, $request); |
|
| 316 | + $data = $this->filter_response_by_context($data, $context); |
|
| 317 | 317 | |
| 318 | - $response = rest_ensure_response( $data ); |
|
| 318 | + $response = rest_ensure_response($data); |
|
| 319 | 319 | |
| 320 | - $response->add_links( $this->prepare_links( $item['id'] ) ); |
|
| 320 | + $response->add_links($this->prepare_links($item['id'])); |
|
| 321 | 321 | |
| 322 | 322 | return $response; |
| 323 | 323 | } |
@@ -330,10 +330,10 @@ discard block |
||
| 330 | 330 | * @param array $setting Setting. |
| 331 | 331 | * @return array |
| 332 | 332 | */ |
| 333 | - public function filter_setting( $setting ) { |
|
| 333 | + public function filter_setting($setting) { |
|
| 334 | 334 | return array_intersect_key( |
| 335 | 335 | $setting, |
| 336 | - array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
| 336 | + array_flip(array_filter(array_keys($setting), array($this, 'allowed_setting_keys'))) |
|
| 337 | 337 | ); |
| 338 | 338 | } |
| 339 | 339 | |
@@ -343,8 +343,8 @@ discard block |
||
| 343 | 343 | * @param string $key Key to check. |
| 344 | 344 | * @return boolean |
| 345 | 345 | */ |
| 346 | - public function allowed_setting_keys( $key ) { |
|
| 347 | - return in_array( $key, array_keys( $this->setting_defaults() ), true ); |
|
| 346 | + public function allowed_setting_keys($key) { |
|
| 347 | + return in_array($key, array_keys($this->setting_defaults()), true); |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
@@ -376,11 +376,11 @@ discard block |
||
| 376 | 376 | * @param array $setting The setting to sanitize. |
| 377 | 377 | * @return array |
| 378 | 378 | */ |
| 379 | - public function sanitize_setting( $setting ) { |
|
| 379 | + public function sanitize_setting($setting) { |
|
| 380 | 380 | |
| 381 | - $setting = wp_parse_args( $setting, $this->setting_defaults() ); |
|
| 382 | - $setting['value'] = wpinv_get_option( $setting['id'], $setting['std'] ); |
|
| 383 | - return $this->filter_setting( $setting ); |
|
| 381 | + $setting = wp_parse_args($setting, $this->setting_defaults()); |
|
| 382 | + $setting['value'] = wpinv_get_option($setting['id'], $setting['std']); |
|
| 383 | + return $this->filter_setting($setting); |
|
| 384 | 384 | |
| 385 | 385 | } |
| 386 | 386 | |
@@ -391,31 +391,31 @@ discard block |
||
| 391 | 391 | * @param string $setting_id Setting ID. |
| 392 | 392 | * @return array|WP_Error |
| 393 | 393 | */ |
| 394 | - public function get_setting( $setting_id ) { |
|
| 394 | + public function get_setting($setting_id) { |
|
| 395 | 395 | |
| 396 | - if ( empty( $setting_id ) ) { |
|
| 397 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 396 | + if (empty($setting_id)) { |
|
| 397 | + return new WP_Error('rest_setting_setting_invalid', __('Invalid setting.', 'invoicing'), array('status' => 404)); |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | - $settings = $this->get_settings(); |
|
| 400 | + $settings = $this->get_settings(); |
|
| 401 | 401 | |
| 402 | - foreach ( $settings as $tabs ) { |
|
| 402 | + foreach ($settings as $tabs) { |
|
| 403 | 403 | |
| 404 | - foreach ( $tabs as $sections ) { |
|
| 404 | + foreach ($tabs as $sections) { |
|
| 405 | 405 | |
| 406 | - if ( isset( $sections[ $setting_id ] ) ) { |
|
| 407 | - if ( ! $this->is_setting_type_valid( $sections[ $setting_id ]['type'] ) ) { |
|
| 408 | - return new WP_Error( 'rest_setting_setting_type_invalid', __( 'Invalid setting type.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 406 | + if (isset($sections[$setting_id])) { |
|
| 407 | + if (!$this->is_setting_type_valid($sections[$setting_id]['type'])) { |
|
| 408 | + return new WP_Error('rest_setting_setting_type_invalid', __('Invalid setting type.', 'invoicing'), array('status' => 404)); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | - return $sections[ $setting_id ]; |
|
| 411 | + return $sections[$setting_id]; |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | - return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 418 | + return new WP_Error('rest_setting_setting_invalid', __('Invalid setting.', 'invoicing'), array('status' => 404)); |
|
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | /** |
@@ -424,41 +424,41 @@ discard block |
||
| 424 | 424 | * @param WP_REST_Request $request Request data. |
| 425 | 425 | * @return array |
| 426 | 426 | */ |
| 427 | - public function get_tabs( $request ) { |
|
| 427 | + public function get_tabs($request) { |
|
| 428 | 428 | $tabs = wpinv_get_settings_tabs(); |
| 429 | 429 | $prepared = array(); |
| 430 | 430 | |
| 431 | - foreach ( $tabs as $id => $tab ) { |
|
| 431 | + foreach ($tabs as $id => $tab) { |
|
| 432 | 432 | |
| 433 | 433 | $_request = $request; |
| 434 | - $_request['tab'] = sanitize_title( $id ); |
|
| 434 | + $_request['tab'] = sanitize_title($id); |
|
| 435 | 435 | $data = array( |
| 436 | - 'id' => sanitize_title( $id ), |
|
| 437 | - 'label' => sanitize_text_field( $tab ), |
|
| 438 | - 'sections' => $this->get_sections( $_request ), |
|
| 436 | + 'id' => sanitize_title($id), |
|
| 437 | + 'label' => sanitize_text_field($tab), |
|
| 438 | + 'sections' => $this->get_sections($_request), |
|
| 439 | 439 | ); |
| 440 | 440 | |
| 441 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 442 | - $response = rest_ensure_response( $data ); |
|
| 441 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 442 | + $response = rest_ensure_response($data); |
|
| 443 | 443 | |
| 444 | - if ( ! is_wp_error( $response ) ) { |
|
| 444 | + if (!is_wp_error($response)) { |
|
| 445 | 445 | $links = array( |
| 446 | 446 | 'sections' => array( |
| 447 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), |
|
| 447 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $id)), |
|
| 448 | 448 | ), |
| 449 | 449 | 'collection' => array( |
| 450 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 450 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
| 451 | 451 | ), |
| 452 | 452 | ); |
| 453 | - $response->add_links( $links ); |
|
| 454 | - $response = $this->prepare_response_for_collection( $response ); |
|
| 453 | + $response->add_links($links); |
|
| 454 | + $response = $this->prepare_response_for_collection($response); |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | $prepared[] = $response; |
| 458 | 458 | |
| 459 | 459 | } |
| 460 | 460 | |
| 461 | - return rest_ensure_response( $prepared ); |
|
| 461 | + return rest_ensure_response($prepared); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -467,43 +467,43 @@ discard block |
||
| 467 | 467 | * @param WP_REST_Request $request Request data. |
| 468 | 468 | * @return array |
| 469 | 469 | */ |
| 470 | - public function get_sections( $request ) { |
|
| 470 | + public function get_sections($request) { |
|
| 471 | 471 | |
| 472 | - $tab = sanitize_title( $request['tab'] ); |
|
| 473 | - $sections = wpinv_get_settings_tab_sections( $tab ); |
|
| 472 | + $tab = sanitize_title($request['tab']); |
|
| 473 | + $sections = wpinv_get_settings_tab_sections($tab); |
|
| 474 | 474 | $prepared = array(); |
| 475 | 475 | |
| 476 | - foreach ( $sections as $id => $section ) { |
|
| 476 | + foreach ($sections as $id => $section) { |
|
| 477 | 477 | |
| 478 | - $data = array( |
|
| 479 | - 'id' => sanitize_title( $id ), |
|
| 480 | - 'label' => sanitize_text_field( $section ), |
|
| 478 | + $data = array( |
|
| 479 | + 'id' => sanitize_title($id), |
|
| 480 | + 'label' => sanitize_text_field($section), |
|
| 481 | 481 | ); |
| 482 | 482 | |
| 483 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 484 | - $response = rest_ensure_response( $data ); |
|
| 483 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 484 | + $response = rest_ensure_response($data); |
|
| 485 | 485 | |
| 486 | - if ( ! is_wp_error( $response ) ) { |
|
| 486 | + if (!is_wp_error($response)) { |
|
| 487 | 487 | $links = array( |
| 488 | 488 | 'settings' => array( |
| 489 | - 'href' => rest_url( sprintf( '/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id ) ), |
|
| 489 | + 'href' => rest_url(sprintf('/%s/%s/%s/%s', $this->namespace, $this->rest_base, $tab, $id)), |
|
| 490 | 490 | ), |
| 491 | 491 | 'collection' => array( |
| 492 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tab ) ), |
|
| 492 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $tab)), |
|
| 493 | 493 | ), |
| 494 | 494 | 'tabs' => array( |
| 495 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
| 495 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
| 496 | 496 | ), |
| 497 | 497 | ); |
| 498 | - $response->add_links( $links ); |
|
| 499 | - $response = $this->prepare_response_for_collection( $response ); |
|
| 498 | + $response->add_links($links); |
|
| 499 | + $response = $this->prepare_response_for_collection($response); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | $prepared[] = $response; |
| 503 | 503 | |
| 504 | 504 | } |
| 505 | 505 | |
| 506 | - return rest_ensure_response( $prepared ); |
|
| 506 | + return rest_ensure_response($prepared); |
|
| 507 | 507 | } |
| 508 | 508 | |
| 509 | 509 | /** |
@@ -513,7 +513,7 @@ discard block |
||
| 513 | 513 | */ |
| 514 | 514 | public function get_settings() { |
| 515 | 515 | |
| 516 | - if ( empty( $this->settings ) ) { |
|
| 516 | + if (empty($this->settings)) { |
|
| 517 | 517 | $this->settings = wpinv_get_registered_settings(); |
| 518 | 518 | } |
| 519 | 519 | |
@@ -528,21 +528,21 @@ discard block |
||
| 528 | 528 | * @param string $type Type. |
| 529 | 529 | * @return bool |
| 530 | 530 | */ |
| 531 | - public function is_setting_type_valid( $type ) { |
|
| 531 | + public function is_setting_type_valid($type) { |
|
| 532 | 532 | |
| 533 | 533 | return in_array( |
| 534 | 534 | $type, array( |
| 535 | - 'text', // Validates with validate_setting_text_field. |
|
| 536 | - 'email', // Validates with validate_setting_text_field. |
|
| 537 | - 'number', // Validates with validate_setting_text_field. |
|
| 538 | - 'color', // Validates with validate_setting_text_field. |
|
| 539 | - 'password', // Validates with validate_setting_text_field. |
|
| 540 | - 'textarea', // Validates with validate_setting_textarea_field. |
|
| 541 | - 'select', // Validates with validate_setting_select_field. |
|
| 542 | - 'multiselect', // Validates with validate_setting_multiselect_field. |
|
| 543 | - 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
| 544 | - 'checkbox', // Validates with validate_setting_checkbox_field. |
|
| 545 | - 'header', // Validates with validate_setting_text_field. |
|
| 535 | + 'text', // Validates with validate_setting_text_field. |
|
| 536 | + 'email', // Validates with validate_setting_text_field. |
|
| 537 | + 'number', // Validates with validate_setting_text_field. |
|
| 538 | + 'color', // Validates with validate_setting_text_field. |
|
| 539 | + 'password', // Validates with validate_setting_text_field. |
|
| 540 | + 'textarea', // Validates with validate_setting_textarea_field. |
|
| 541 | + 'select', // Validates with validate_setting_select_field. |
|
| 542 | + 'multiselect', // Validates with validate_setting_multiselect_field. |
|
| 543 | + 'radio', // Validates with validate_setting_radio_field (-> validate_setting_select_field). |
|
| 544 | + 'checkbox', // Validates with validate_setting_checkbox_field. |
|
| 545 | + 'header', // Validates with validate_setting_text_field. |
|
| 546 | 546 | ) |
| 547 | 547 | ); |
| 548 | 548 | |
@@ -556,8 +556,8 @@ discard block |
||
| 556 | 556 | public function get_item_schema() { |
| 557 | 557 | |
| 558 | 558 | // Maybe retrieve the schema from cache. |
| 559 | - if ( ! empty( $this->schema ) ) { |
|
| 560 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 559 | + if (!empty($this->schema)) { |
|
| 560 | + return $this->add_additional_fields_schema($this->schema); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | $schema = array( |
@@ -566,105 +566,105 @@ discard block |
||
| 566 | 566 | 'type' => 'object', |
| 567 | 567 | 'properties' => array( |
| 568 | 568 | 'id' => array( |
| 569 | - 'description' => __( 'A unique identifier for the setting.', 'invoicing' ), |
|
| 569 | + 'description' => __('A unique identifier for the setting.', 'invoicing'), |
|
| 570 | 570 | 'type' => 'string', |
| 571 | 571 | 'arg_options' => array( |
| 572 | 572 | 'sanitize_callback' => 'sanitize_title', |
| 573 | 573 | ), |
| 574 | - 'context' => array( 'view', 'edit' ), |
|
| 574 | + 'context' => array('view', 'edit'), |
|
| 575 | 575 | 'readonly' => true, |
| 576 | 576 | ), |
| 577 | 577 | 'tab' => array( |
| 578 | - 'description' => __( 'An identifier for the tab this setting belongs to.', 'invoicing' ), |
|
| 578 | + 'description' => __('An identifier for the tab this setting belongs to.', 'invoicing'), |
|
| 579 | 579 | 'type' => 'string', |
| 580 | 580 | 'arg_options' => array( |
| 581 | 581 | 'sanitize_callback' => 'sanitize_title', |
| 582 | 582 | ), |
| 583 | - 'context' => array( 'view', 'edit' ), |
|
| 583 | + 'context' => array('view', 'edit'), |
|
| 584 | 584 | 'readonly' => true, |
| 585 | 585 | ), |
| 586 | 586 | 'section' => array( |
| 587 | - 'description' => __( 'An identifier for the section this setting belongs to.', 'invoicing' ), |
|
| 587 | + 'description' => __('An identifier for the section this setting belongs to.', 'invoicing'), |
|
| 588 | 588 | 'type' => 'string', |
| 589 | 589 | 'arg_options' => array( |
| 590 | 590 | 'sanitize_callback' => 'sanitize_title', |
| 591 | 591 | ), |
| 592 | - 'context' => array( 'view', 'edit' ), |
|
| 592 | + 'context' => array('view', 'edit'), |
|
| 593 | 593 | 'readonly' => true, |
| 594 | 594 | ), |
| 595 | 595 | 'name' => array( |
| 596 | - 'description' => __( 'A human readable label for the setting used in interfaces.', 'invoicing' ), |
|
| 596 | + 'description' => __('A human readable label for the setting used in interfaces.', 'invoicing'), |
|
| 597 | 597 | 'type' => 'string', |
| 598 | 598 | 'arg_options' => array( |
| 599 | 599 | 'sanitize_callback' => 'sanitize_text_field', |
| 600 | 600 | ), |
| 601 | - 'context' => array( 'view', 'edit' ), |
|
| 601 | + 'context' => array('view', 'edit'), |
|
| 602 | 602 | 'readonly' => true, |
| 603 | 603 | ), |
| 604 | 604 | 'desc' => array( |
| 605 | - 'description' => __( 'A human readable description for the setting used in interfaces.', 'invoicing' ), |
|
| 605 | + 'description' => __('A human readable description for the setting used in interfaces.', 'invoicing'), |
|
| 606 | 606 | 'type' => 'string', |
| 607 | - 'context' => array( 'view', 'edit' ), |
|
| 607 | + 'context' => array('view', 'edit'), |
|
| 608 | 608 | 'readonly' => true, |
| 609 | 609 | ), |
| 610 | 610 | 'value' => array( |
| 611 | - 'description' => __( 'The current value of this setting.', 'invoicing' ), |
|
| 611 | + 'description' => __('The current value of this setting.', 'invoicing'), |
|
| 612 | 612 | 'type' => 'mixed', |
| 613 | - 'context' => array( 'view', 'edit' ), |
|
| 613 | + 'context' => array('view', 'edit'), |
|
| 614 | 614 | ), |
| 615 | 615 | 'default' => array( |
| 616 | - 'description' => __( 'Default value for the setting.', 'invoicing' ), |
|
| 616 | + 'description' => __('Default value for the setting.', 'invoicing'), |
|
| 617 | 617 | 'type' => 'mixed', |
| 618 | - 'context' => array( 'view', 'edit' ), |
|
| 618 | + 'context' => array('view', 'edit'), |
|
| 619 | 619 | 'readonly' => true, |
| 620 | 620 | ), |
| 621 | 621 | 'placeholder' => array( |
| 622 | - 'description' => __( 'Placeholder text to be displayed in text inputs.', 'invoicing' ), |
|
| 622 | + 'description' => __('Placeholder text to be displayed in text inputs.', 'invoicing'), |
|
| 623 | 623 | 'type' => 'string', |
| 624 | 624 | 'arg_options' => array( |
| 625 | 625 | 'sanitize_callback' => 'sanitize_text_field', |
| 626 | 626 | ), |
| 627 | - 'context' => array( 'view', 'edit' ), |
|
| 627 | + 'context' => array('view', 'edit'), |
|
| 628 | 628 | 'readonly' => true, |
| 629 | 629 | ), |
| 630 | 630 | 'type' => array( |
| 631 | - 'description' => __( 'Type of setting.', 'invoicing' ), |
|
| 631 | + 'description' => __('Type of setting.', 'invoicing'), |
|
| 632 | 632 | 'type' => 'string', |
| 633 | 633 | 'arg_options' => array( |
| 634 | 634 | 'sanitize_callback' => 'sanitize_text_field', |
| 635 | 635 | ), |
| 636 | - 'context' => array( 'view', 'edit' ), |
|
| 637 | - 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html' ), |
|
| 636 | + 'context' => array('view', 'edit'), |
|
| 637 | + 'enum' => array('text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox', 'raw_html'), |
|
| 638 | 638 | 'readonly' => true, |
| 639 | 639 | ), |
| 640 | 640 | 'options' => array( |
| 641 | - 'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing' ), |
|
| 641 | + 'description' => __('Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'invoicing'), |
|
| 642 | 642 | 'type' => 'object', |
| 643 | - 'context' => array( 'view', 'edit' ), |
|
| 643 | + 'context' => array('view', 'edit'), |
|
| 644 | 644 | 'readonly' => true, |
| 645 | 645 | ), |
| 646 | 646 | 'readonly' => array( |
| 647 | - 'description' => __( 'Whether or not this setting is readonly', 'invoicing' ), |
|
| 647 | + 'description' => __('Whether or not this setting is readonly', 'invoicing'), |
|
| 648 | 648 | 'type' => 'string', |
| 649 | - 'context' => array( 'view' ), |
|
| 649 | + 'context' => array('view'), |
|
| 650 | 650 | 'readonly' => true, |
| 651 | 651 | ), |
| 652 | 652 | 'faux' => array( |
| 653 | - 'description' => __( 'Whether or not this setting is readonly/faux', 'invoicing' ), |
|
| 653 | + 'description' => __('Whether or not this setting is readonly/faux', 'invoicing'), |
|
| 654 | 654 | 'type' => 'string', |
| 655 | - 'context' => array( 'view' ), |
|
| 655 | + 'context' => array('view'), |
|
| 656 | 656 | 'readonly' => true, |
| 657 | 657 | ), |
| 658 | 658 | ), |
| 659 | 659 | ); |
| 660 | 660 | |
| 661 | 661 | // Filters the settings schema for the REST API. |
| 662 | - $schema = apply_filters( 'getpaid_rest_settings_schema', $schema ); |
|
| 662 | + $schema = apply_filters('getpaid_rest_settings_schema', $schema); |
|
| 663 | 663 | |
| 664 | 664 | // Cache the settings schema. |
| 665 | 665 | $this->schema = $schema; |
| 666 | 666 | |
| 667 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 667 | + return $this->add_additional_fields_schema($this->schema); |
|
| 668 | 668 | |
| 669 | 669 | } |
| 670 | 670 | |
@@ -676,9 +676,9 @@ discard block |
||
| 676 | 676 | * @param array $setting Setting. |
| 677 | 677 | * @return string |
| 678 | 678 | */ |
| 679 | - public function validate_setting_text_field( $value ) { |
|
| 680 | - $value = is_null( $value ) ? '' : $value; |
|
| 681 | - return wp_kses_post( trim( stripslashes( $value ) ) ); |
|
| 679 | + public function validate_setting_text_field($value) { |
|
| 680 | + $value = is_null($value) ? '' : $value; |
|
| 681 | + return wp_kses_post(trim(stripslashes($value))); |
|
| 682 | 682 | } |
| 683 | 683 | |
| 684 | 684 | /** |
@@ -689,11 +689,11 @@ discard block |
||
| 689 | 689 | * @param array $setting Setting. |
| 690 | 690 | * @return string|WP_Error |
| 691 | 691 | */ |
| 692 | - public function validate_setting_select_field( $value, $setting ) { |
|
| 693 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 692 | + public function validate_setting_select_field($value, $setting) { |
|
| 693 | + if (array_key_exists($value, $setting['options'])) { |
|
| 694 | 694 | return $value; |
| 695 | 695 | } else { |
| 696 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 696 | + return new WP_Error('rest_setting_value_invalid', __('An invalid setting value was passed.', 'invoicing'), array('status' => 400)); |
|
| 697 | 697 | } |
| 698 | 698 | } |
| 699 | 699 | |
@@ -705,18 +705,18 @@ discard block |
||
| 705 | 705 | * @param array $setting Setting. |
| 706 | 706 | * @return array|WP_Error |
| 707 | 707 | */ |
| 708 | - public function validate_setting_multiselect_field( $values, $setting ) { |
|
| 709 | - if ( empty( $values ) ) { |
|
| 708 | + public function validate_setting_multiselect_field($values, $setting) { |
|
| 709 | + if (empty($values)) { |
|
| 710 | 710 | return array(); |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if ( ! is_array( $values ) ) { |
|
| 714 | - return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 713 | + if (!is_array($values)) { |
|
| 714 | + return new WP_Error('rest_setting_value_invalid', __('An invalid setting value was passed.', 'invoicing'), array('status' => 400)); |
|
| 715 | 715 | } |
| 716 | 716 | |
| 717 | 717 | $final_values = array(); |
| 718 | - foreach ( $values as $value ) { |
|
| 719 | - if ( array_key_exists( $value, $setting['options'] ) ) { |
|
| 718 | + foreach ($values as $value) { |
|
| 719 | + if (array_key_exists($value, $setting['options'])) { |
|
| 720 | 720 | $final_values[] = $value; |
| 721 | 721 | } |
| 722 | 722 | } |
@@ -732,8 +732,8 @@ discard block |
||
| 732 | 732 | * @param array $setting Setting. |
| 733 | 733 | * @return string|WP_Error |
| 734 | 734 | */ |
| 735 | - public function validate_setting_radio_field( $value, $setting ) { |
|
| 736 | - return $this->validate_setting_select_field( $value, $setting ); |
|
| 735 | + public function validate_setting_radio_field($value, $setting) { |
|
| 736 | + return $this->validate_setting_select_field($value, $setting); |
|
| 737 | 737 | } |
| 738 | 738 | |
| 739 | 739 | /** |
@@ -743,8 +743,8 @@ discard block |
||
| 743 | 743 | * @param string $value Value. |
| 744 | 744 | * @return int |
| 745 | 745 | */ |
| 746 | - public function validate_setting_checkbox_field( $value ) { |
|
| 747 | - return (int) ! empty( $value ); |
|
| 746 | + public function validate_setting_checkbox_field($value) { |
|
| 747 | + return (int) !empty($value); |
|
| 748 | 748 | } |
| 749 | 749 | |
| 750 | 750 | /** |
@@ -754,10 +754,10 @@ discard block |
||
| 754 | 754 | * @param string $value Value. |
| 755 | 755 | * @return string |
| 756 | 756 | */ |
| 757 | - public function validate_setting_textarea_field( $value ) { |
|
| 758 | - $value = is_null( $value ) ? '' : $value; |
|
| 757 | + public function validate_setting_textarea_field($value) { |
|
| 758 | + $value = is_null($value) ? '' : $value; |
|
| 759 | 759 | return wp_kses( |
| 760 | - trim( stripslashes( $value ) ), |
|
| 760 | + trim(stripslashes($value)), |
|
| 761 | 761 | array_merge( |
| 762 | 762 | array( |
| 763 | 763 | 'iframe' => array( |
@@ -767,7 +767,7 @@ discard block |
||
| 767 | 767 | 'class' => true, |
| 768 | 768 | ), |
| 769 | 769 | ), |
| 770 | - wp_kses_allowed_html( 'post' ) |
|
| 770 | + wp_kses_allowed_html('post') |
|
| 771 | 771 | ) |
| 772 | 772 | ); |
| 773 | 773 | } |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | * @since 1.0.0 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; |
|
| 9 | +defined('ABSPATH') || exit; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Retrieves all default settings. |
@@ -16,13 +16,13 @@ discard block |
||
| 16 | 16 | function wpinv_get_settings() { |
| 17 | 17 | $defaults = array(); |
| 18 | 18 | |
| 19 | - foreach ( array_values( wpinv_get_registered_settings() ) as $tab_settings ) { |
|
| 19 | + foreach (array_values(wpinv_get_registered_settings()) as $tab_settings) { |
|
| 20 | 20 | |
| 21 | - foreach ( array_values( $tab_settings ) as $section_settings ) { |
|
| 21 | + foreach (array_values($tab_settings) as $section_settings) { |
|
| 22 | 22 | |
| 23 | - foreach ( $section_settings as $key => $setting ) { |
|
| 24 | - if ( isset( $setting['std'] ) ) { |
|
| 25 | - $defaults[ $key ] = $setting['std']; |
|
| 23 | + foreach ($section_settings as $key => $setting) { |
|
| 24 | + if (isset($setting['std'])) { |
|
| 25 | + $defaults[$key] = $setting['std']; |
|
| 26 | 26 | } |
| 27 | 27 | } |
| 28 | 28 | |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | global $wpinv_options; |
| 44 | 44 | |
| 45 | 45 | // Try fetching the saved options. |
| 46 | - if ( ! is_array( $wpinv_options ) ) { |
|
| 47 | - $wpinv_options = get_option( 'wpinv_settings' ); |
|
| 46 | + if (!is_array($wpinv_options)) { |
|
| 47 | + $wpinv_options = get_option('wpinv_settings'); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // If that fails, don't fetch the default settings to prevent a loop. |
| 51 | - if ( ! is_array( $wpinv_options ) ) { |
|
| 51 | + if (!is_array($wpinv_options)) { |
|
| 52 | 52 | $wpinv_options = array(); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -62,13 +62,13 @@ discard block |
||
| 62 | 62 | * @param mixed $default The default value to use if the setting has not been set. |
| 63 | 63 | * @return mixed |
| 64 | 64 | */ |
| 65 | -function wpinv_get_option( $key = '', $default = false ) { |
|
| 65 | +function wpinv_get_option($key = '', $default = false) { |
|
| 66 | 66 | |
| 67 | 67 | $options = wpinv_get_options(); |
| 68 | - $value = isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 69 | - $value = apply_filters( 'wpinv_get_option', $value, $key, $default ); |
|
| 68 | + $value = isset($options[$key]) ? $options[$key] : $default; |
|
| 69 | + $value = apply_filters('wpinv_get_option', $value, $key, $default); |
|
| 70 | 70 | |
| 71 | - return apply_filters( 'wpinv_get_option_' . $key, $value, $key, $default ); |
|
| 71 | + return apply_filters('wpinv_get_option_' . $key, $value, $key, $default); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | * @param array $options the new options. |
| 78 | 78 | * @return bool |
| 79 | 79 | */ |
| 80 | -function wpinv_update_options( $options ) { |
|
| 80 | +function wpinv_update_options($options) { |
|
| 81 | 81 | global $wpinv_options; |
| 82 | 82 | |
| 83 | 83 | // update the option. |
| 84 | - if ( is_array( $options ) && update_option( 'wpinv_settings', $options ) ) { |
|
| 84 | + if (is_array($options) && update_option('wpinv_settings', $options)) { |
|
| 85 | 85 | $wpinv_options = $options; |
| 86 | 86 | return true; |
| 87 | 87 | } |
@@ -96,24 +96,24 @@ discard block |
||
| 96 | 96 | * @param mixed $value The setting value. |
| 97 | 97 | * @return bool |
| 98 | 98 | */ |
| 99 | -function wpinv_update_option( $key = '', $value = false ) { |
|
| 99 | +function wpinv_update_option($key = '', $value = false) { |
|
| 100 | 100 | |
| 101 | 101 | // If no key, exit. |
| 102 | - if ( empty( $key ) ) { |
|
| 102 | + if (empty($key)) { |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Maybe delete the option instead. |
| 107 | - if ( is_null( $value ) ) { |
|
| 108 | - return wpinv_delete_option( $key ); |
|
| 107 | + if (is_null($value)) { |
|
| 108 | + return wpinv_delete_option($key); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Prepare the new options. |
| 112 | 112 | $options = wpinv_get_options(); |
| 113 | - $options[ $key ] = apply_filters( 'wpinv_update_option', $value, $key ); |
|
| 113 | + $options[$key] = apply_filters('wpinv_update_option', $value, $key); |
|
| 114 | 114 | |
| 115 | 115 | // Save the new options. |
| 116 | - return wpinv_update_options( $options ); |
|
| 116 | + return wpinv_update_options($options); |
|
| 117 | 117 | |
| 118 | 118 | } |
| 119 | 119 | |
@@ -123,18 +123,18 @@ discard block |
||
| 123 | 123 | * @param string $key the setting key. |
| 124 | 124 | * @return bool |
| 125 | 125 | */ |
| 126 | -function wpinv_delete_option( $key = '' ) { |
|
| 126 | +function wpinv_delete_option($key = '') { |
|
| 127 | 127 | |
| 128 | 128 | // If no key, exit |
| 129 | - if ( empty( $key ) ) { |
|
| 129 | + if (empty($key)) { |
|
| 130 | 130 | return false; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | $options = wpinv_get_options(); |
| 134 | 134 | |
| 135 | - if ( isset( $options[ $key ] ) ) { |
|
| 136 | - unset( $options[ $key ] ); |
|
| 137 | - return wpinv_update_options( $options ); |
|
| 135 | + if (isset($options[$key])) { |
|
| 136 | + unset($options[$key]); |
|
| 137 | + return wpinv_update_options($options); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | return true; |
@@ -148,14 +148,14 @@ discard block |
||
| 148 | 148 | function wpinv_register_settings() { |
| 149 | 149 | |
| 150 | 150 | // Loop through all tabs. |
| 151 | - foreach ( wpinv_get_registered_settings() as $tab => $sections ) { |
|
| 151 | + foreach (wpinv_get_registered_settings() as $tab => $sections) { |
|
| 152 | 152 | |
| 153 | 153 | // In each tab, loop through sections. |
| 154 | - foreach ( $sections as $section => $settings ) { |
|
| 154 | + foreach ($sections as $section => $settings) { |
|
| 155 | 155 | |
| 156 | 156 | // Check for backwards compatibility |
| 157 | - $section_tabs = wpinv_get_settings_tab_sections( $tab ); |
|
| 158 | - if ( ! is_array( $section_tabs ) || ! array_key_exists( $section, $section_tabs ) ) { |
|
| 157 | + $section_tabs = wpinv_get_settings_tab_sections($tab); |
|
| 158 | + if (!is_array($section_tabs) || !array_key_exists($section, $section_tabs)) { |
|
| 159 | 159 | $section = 'main'; |
| 160 | 160 | $settings = $sections; |
| 161 | 161 | } |
@@ -168,9 +168,9 @@ discard block |
||
| 168 | 168 | 'wpinv_settings_' . $tab . '_' . $section |
| 169 | 169 | ); |
| 170 | 170 | |
| 171 | - foreach ( $settings as $option ) { |
|
| 172 | - if ( ! empty( $option['id'] ) ) { |
|
| 173 | - wpinv_register_settings_option( $tab, $section, $option ); |
|
| 171 | + foreach ($settings as $option) { |
|
| 172 | + if (!empty($option['id'])) { |
|
| 173 | + wpinv_register_settings_option($tab, $section, $option); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
@@ -178,9 +178,9 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | // Creates our settings in the options table. |
| 181 | - register_setting( 'wpinv_settings', 'wpinv_settings', 'wpinv_settings_sanitize' ); |
|
| 181 | + register_setting('wpinv_settings', 'wpinv_settings', 'wpinv_settings_sanitize'); |
|
| 182 | 182 | } |
| 183 | -add_action( 'admin_init', 'wpinv_register_settings' ); |
|
| 183 | +add_action('admin_init', 'wpinv_register_settings'); |
|
| 184 | 184 | |
| 185 | 185 | /** |
| 186 | 186 | * Register a single settings option. |
@@ -190,47 +190,47 @@ discard block |
||
| 190 | 190 | * @param string $option |
| 191 | 191 | * |
| 192 | 192 | */ |
| 193 | -function wpinv_register_settings_option( $tab, $section, $option ) { |
|
| 193 | +function wpinv_register_settings_option($tab, $section, $option) { |
|
| 194 | 194 | |
| 195 | - $name = isset( $option['name'] ) ? $option['name'] : ''; |
|
| 195 | + $name = isset($option['name']) ? $option['name'] : ''; |
|
| 196 | 196 | $cb = "wpinv_{$option['type']}_callback"; |
| 197 | 197 | $section = "wpinv_settings_{$tab}_$section"; |
| 198 | 198 | |
| 199 | - if ( isset( $option['desc'] ) && ! empty( $option['help-tip'] ) ) { |
|
| 200 | - $tip = wpinv_clean( $option['desc'] ); |
|
| 199 | + if (isset($option['desc']) && !empty($option['help-tip'])) { |
|
| 200 | + $tip = wpinv_clean($option['desc']); |
|
| 201 | 201 | $name .= "<span class='dashicons dashicons-editor-help wpi-help-tip' title='$tip'></span>"; |
| 202 | - unset( $option['desc'] ); |
|
| 202 | + unset($option['desc']); |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | // Loop through all tabs. |
| 206 | 206 | add_settings_field( |
| 207 | 207 | 'wpinv_settings[' . $option['id'] . ']', |
| 208 | 208 | $name, |
| 209 | - function_exists( $cb ) ? $cb : 'wpinv_missing_callback', |
|
| 209 | + function_exists($cb) ? $cb : 'wpinv_missing_callback', |
|
| 210 | 210 | $section, |
| 211 | 211 | $section, |
| 212 | 212 | array( |
| 213 | 213 | 'section' => $section, |
| 214 | - 'id' => isset( $option['id'] ) ? $option['id'] : uniqid( 'wpinv-' ), |
|
| 215 | - 'desc' => isset( $option['desc'] ) ? $option['desc'] : '', |
|
| 214 | + 'id' => isset($option['id']) ? $option['id'] : uniqid('wpinv-'), |
|
| 215 | + 'desc' => isset($option['desc']) ? $option['desc'] : '', |
|
| 216 | 216 | 'name' => $name, |
| 217 | - 'size' => isset( $option['size'] ) ? $option['size'] : null, |
|
| 218 | - 'options' => isset( $option['options'] ) ? $option['options'] : '', |
|
| 219 | - 'selected' => isset( $option['selected'] ) ? $option['selected'] : null, |
|
| 220 | - 'std' => isset( $option['std'] ) ? $option['std'] : '', |
|
| 221 | - 'min' => isset( $option['min'] ) ? $option['min'] : 0, |
|
| 222 | - 'max' => isset( $option['max'] ) ? $option['max'] : 999999, |
|
| 223 | - 'step' => isset( $option['step'] ) ? $option['step'] : 1, |
|
| 224 | - 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null, |
|
| 225 | - 'allow_blank' => isset( $option['allow_blank'] ) ? $option['allow_blank'] : true, |
|
| 226 | - 'readonly' => isset( $option['readonly'] ) ? $option['readonly'] : false, |
|
| 227 | - 'faux' => isset( $option['faux'] ) ? $option['faux'] : false, |
|
| 228 | - 'onchange' => isset( $option['onchange'] ) ? $option['onchange'] : '', |
|
| 229 | - 'custom' => isset( $option['custom'] ) ? $option['custom'] : '', |
|
| 230 | - 'class' => isset( $option['class'] ) ? $option['class'] : '', |
|
| 231 | - 'style' => isset( $option['style'] ) ? $option['style'] : '', |
|
| 232 | - 'cols' => isset( $option['cols'] ) && (int) $option['cols'] > 0 ? (int) $option['cols'] : 50, |
|
| 233 | - 'rows' => isset( $option['rows'] ) && (int) $option['rows'] > 0 ? (int) $option['rows'] : 5, |
|
| 217 | + 'size' => isset($option['size']) ? $option['size'] : null, |
|
| 218 | + 'options' => isset($option['options']) ? $option['options'] : '', |
|
| 219 | + 'selected' => isset($option['selected']) ? $option['selected'] : null, |
|
| 220 | + 'std' => isset($option['std']) ? $option['std'] : '', |
|
| 221 | + 'min' => isset($option['min']) ? $option['min'] : 0, |
|
| 222 | + 'max' => isset($option['max']) ? $option['max'] : 999999, |
|
| 223 | + 'step' => isset($option['step']) ? $option['step'] : 1, |
|
| 224 | + 'placeholder' => isset($option['placeholder']) ? $option['placeholder'] : null, |
|
| 225 | + 'allow_blank' => isset($option['allow_blank']) ? $option['allow_blank'] : true, |
|
| 226 | + 'readonly' => isset($option['readonly']) ? $option['readonly'] : false, |
|
| 227 | + 'faux' => isset($option['faux']) ? $option['faux'] : false, |
|
| 228 | + 'onchange' => isset($option['onchange']) ? $option['onchange'] : '', |
|
| 229 | + 'custom' => isset($option['custom']) ? $option['custom'] : '', |
|
| 230 | + 'class' => isset($option['class']) ? $option['class'] : '', |
|
| 231 | + 'style' => isset($option['style']) ? $option['style'] : '', |
|
| 232 | + 'cols' => isset($option['cols']) && (int) $option['cols'] > 0 ? (int) $option['cols'] : 50, |
|
| 233 | + 'rows' => isset($option['rows']) && (int) $option['rows'] > 0 ? (int) $option['rows'] : 5, |
|
| 234 | 234 | ) |
| 235 | 235 | ); |
| 236 | 236 | |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | * @return array |
| 243 | 243 | */ |
| 244 | 244 | function wpinv_get_registered_settings() { |
| 245 | - return array_filter( apply_filters( 'wpinv_registered_settings', wpinv_get_data( 'admin-settings' ) ) ); |
|
| 245 | + return array_filter(apply_filters('wpinv_registered_settings', wpinv_get_data('admin-settings'))); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | * @return array |
| 252 | 252 | */ |
| 253 | 253 | function getpaid_get_integration_settings() { |
| 254 | - return apply_filters( 'getpaid_integration_settings', array() ); |
|
| 254 | + return apply_filters('getpaid_integration_settings', array()); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** |
@@ -259,139 +259,139 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @return array |
| 261 | 261 | */ |
| 262 | -function wpinv_settings_sanitize( $input = array() ) { |
|
| 262 | +function wpinv_settings_sanitize($input = array()) { |
|
| 263 | 263 | |
| 264 | 264 | $wpinv_options = wpinv_get_options(); |
| 265 | 265 | |
| 266 | - if ( empty( wp_get_raw_referer() ) ) { |
|
| 266 | + if (empty(wp_get_raw_referer())) { |
|
| 267 | 267 | return $input; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - wp_parse_str( wp_get_raw_referer(), $referrer ); |
|
| 270 | + wp_parse_str(wp_get_raw_referer(), $referrer); |
|
| 271 | 271 | |
| 272 | 272 | $settings = wpinv_get_registered_settings(); |
| 273 | - $tab = isset( $referrer['tab'] ) ? $referrer['tab'] : 'general'; |
|
| 274 | - $section = isset( $referrer['section'] ) ? $referrer['section'] : 'main'; |
|
| 273 | + $tab = isset($referrer['tab']) ? $referrer['tab'] : 'general'; |
|
| 274 | + $section = isset($referrer['section']) ? $referrer['section'] : 'main'; |
|
| 275 | 275 | |
| 276 | 276 | $input = $input ? $input : array(); |
| 277 | - $input = apply_filters( 'wpinv_settings_tab_' . $tab . '_sanitize', $input ); |
|
| 278 | - $input = apply_filters( 'wpinv_settings_' . $tab . '-' . $section . '_sanitize', $input ); |
|
| 277 | + $input = apply_filters('wpinv_settings_tab_' . $tab . '_sanitize', $input); |
|
| 278 | + $input = apply_filters('wpinv_settings_' . $tab . '-' . $section . '_sanitize', $input); |
|
| 279 | 279 | |
| 280 | 280 | // Loop through each setting being saved and pass it through a sanitization filter |
| 281 | - foreach ( $input as $key => $value ) { |
|
| 281 | + foreach ($input as $key => $value) { |
|
| 282 | 282 | |
| 283 | 283 | // Get the setting type (checkbox, select, etc) |
| 284 | - $type = isset( $settings[ $tab ][$section][ $key ]['type'] ) ? $settings[ $tab ][$section][ $key ]['type'] : false; |
|
| 284 | + $type = isset($settings[$tab][$section][$key]['type']) ? $settings[$tab][$section][$key]['type'] : false; |
|
| 285 | 285 | |
| 286 | - if ( $type ) { |
|
| 286 | + if ($type) { |
|
| 287 | 287 | // Field type specific filter |
| 288 | - $input[$key] = apply_filters( 'wpinv_settings_sanitize_' . $type, $value, $key ); |
|
| 288 | + $input[$key] = apply_filters('wpinv_settings_sanitize_' . $type, $value, $key); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // General filter |
| 292 | - $input[ $key ] = apply_filters( 'wpinv_settings_sanitize', $input[ $key ], $key ); |
|
| 292 | + $input[$key] = apply_filters('wpinv_settings_sanitize', $input[$key], $key); |
|
| 293 | 293 | |
| 294 | 294 | // Key specific filter. |
| 295 | - $input[ $key ] = apply_filters( "wpinv_settings_sanitize_$key", $input[ $key ] ); |
|
| 295 | + $input[$key] = apply_filters("wpinv_settings_sanitize_$key", $input[$key]); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | // Loop through the whitelist and unset any that are empty for the tab being saved |
| 299 | - $main_settings = $section == 'main' ? $settings[ $tab ] : array(); // Check for extensions that aren't using new sections |
|
| 300 | - $section_settings = ! empty( $settings[ $tab ][ $section ] ) ? $settings[ $tab ][ $section ] : array(); |
|
| 299 | + $main_settings = $section == 'main' ? $settings[$tab] : array(); // Check for extensions that aren't using new sections |
|
| 300 | + $section_settings = !empty($settings[$tab][$section]) ? $settings[$tab][$section] : array(); |
|
| 301 | 301 | |
| 302 | - $found_settings = array_merge( $main_settings, $section_settings ); |
|
| 302 | + $found_settings = array_merge($main_settings, $section_settings); |
|
| 303 | 303 | |
| 304 | - if ( ! empty( $found_settings ) ) { |
|
| 305 | - foreach ( $found_settings as $key => $value ) { |
|
| 304 | + if (!empty($found_settings)) { |
|
| 305 | + foreach ($found_settings as $key => $value) { |
|
| 306 | 306 | |
| 307 | 307 | // settings used to have numeric keys, now they have keys that match the option ID. This ensures both methods work |
| 308 | - if ( is_numeric( $key ) ) { |
|
| 308 | + if (is_numeric($key)) { |
|
| 309 | 309 | $key = $value['id']; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - if ( ! isset( $input[ $key ] ) && isset( $wpinv_options[ $key ] ) ) { |
|
| 313 | - unset( $wpinv_options[ $key ] ); |
|
| 312 | + if (!isset($input[$key]) && isset($wpinv_options[$key])) { |
|
| 313 | + unset($wpinv_options[$key]); |
|
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | // Merge our new settings with the existing |
| 319 | - $output = array_merge( $wpinv_options, $input ); |
|
| 319 | + $output = array_merge($wpinv_options, $input); |
|
| 320 | 320 | |
| 321 | - add_settings_error( 'wpinv-notices', '', __( 'Settings updated.', 'invoicing' ), 'updated' ); |
|
| 321 | + add_settings_error('wpinv-notices', '', __('Settings updated.', 'invoicing'), 'updated'); |
|
| 322 | 322 | |
| 323 | 323 | return $output; |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | -function wpinv_settings_sanitize_misc_accounting( $input ) { |
|
| 326 | +function wpinv_settings_sanitize_misc_accounting($input) { |
|
| 327 | 327 | |
| 328 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 328 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 329 | 329 | return $input; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - if( ! empty( $input['enable_sequential'] ) && !wpinv_get_option( 'enable_sequential' ) ) { |
|
| 332 | + if (!empty($input['enable_sequential']) && !wpinv_get_option('enable_sequential')) { |
|
| 333 | 333 | // Shows an admin notice about upgrading previous order numbers |
| 334 | - getpaid_session()->set( 'upgrade_sequential', '1' ); |
|
| 334 | + getpaid_session()->set('upgrade_sequential', '1'); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | return $input; |
| 338 | 338 | } |
| 339 | -add_filter( 'wpinv_settings_misc-accounting_sanitize', 'wpinv_settings_sanitize_misc_accounting' ); |
|
| 339 | +add_filter('wpinv_settings_misc-accounting_sanitize', 'wpinv_settings_sanitize_misc_accounting'); |
|
| 340 | 340 | |
| 341 | -function wpinv_settings_sanitize_tax_rates( $input ) { |
|
| 342 | - if( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 341 | +function wpinv_settings_sanitize_tax_rates($input) { |
|
| 342 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 343 | 343 | return $input; |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | - $new_rates = ! empty( $_POST['tax_rates'] ) ? array_values( $_POST['tax_rates'] ) : array(); |
|
| 346 | + $new_rates = !empty($_POST['tax_rates']) ? array_values($_POST['tax_rates']) : array(); |
|
| 347 | 347 | $tax_rates = array(); |
| 348 | 348 | |
| 349 | - foreach ( $new_rates as $rate ) { |
|
| 349 | + foreach ($new_rates as $rate) { |
|
| 350 | 350 | |
| 351 | - $rate['rate'] = wpinv_sanitize_amount( $rate['rate'] ); |
|
| 352 | - $rate['name'] = sanitize_text_field( $rate['name'] ); |
|
| 353 | - $rate['state'] = sanitize_text_field( $rate['state'] ); |
|
| 354 | - $rate['country'] = sanitize_text_field( $rate['country'] ); |
|
| 355 | - $rate['global'] = empty( $rate['state'] ); |
|
| 351 | + $rate['rate'] = wpinv_sanitize_amount($rate['rate']); |
|
| 352 | + $rate['name'] = sanitize_text_field($rate['name']); |
|
| 353 | + $rate['state'] = sanitize_text_field($rate['state']); |
|
| 354 | + $rate['country'] = sanitize_text_field($rate['country']); |
|
| 355 | + $rate['global'] = empty($rate['state']); |
|
| 356 | 356 | $tax_rates[] = $rate; |
| 357 | 357 | |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | - update_option( 'wpinv_tax_rates', $tax_rates ); |
|
| 360 | + update_option('wpinv_tax_rates', $tax_rates); |
|
| 361 | 361 | |
| 362 | 362 | return $input; |
| 363 | 363 | } |
| 364 | -add_filter( 'wpinv_settings_taxes-rates_sanitize', 'wpinv_settings_sanitize_tax_rates' ); |
|
| 364 | +add_filter('wpinv_settings_taxes-rates_sanitize', 'wpinv_settings_sanitize_tax_rates'); |
|
| 365 | 365 | |
| 366 | -function wpinv_sanitize_text_field( $input ) { |
|
| 367 | - return trim( $input ); |
|
| 366 | +function wpinv_sanitize_text_field($input) { |
|
| 367 | + return trim($input); |
|
| 368 | 368 | } |
| 369 | -add_filter( 'wpinv_settings_sanitize_text', 'wpinv_sanitize_text_field' ); |
|
| 369 | +add_filter('wpinv_settings_sanitize_text', 'wpinv_sanitize_text_field'); |
|
| 370 | 370 | |
| 371 | 371 | function wpinv_get_settings_tabs() { |
| 372 | 372 | $tabs = array(); |
| 373 | - $tabs['general'] = __( 'General', 'invoicing' ); |
|
| 374 | - $tabs['gateways'] = __( 'Payment Gateways', 'invoicing' ); |
|
| 375 | - $tabs['taxes'] = __( 'Taxes', 'invoicing' ); |
|
| 376 | - $tabs['emails'] = __( 'Emails', 'invoicing' ); |
|
| 373 | + $tabs['general'] = __('General', 'invoicing'); |
|
| 374 | + $tabs['gateways'] = __('Payment Gateways', 'invoicing'); |
|
| 375 | + $tabs['taxes'] = __('Taxes', 'invoicing'); |
|
| 376 | + $tabs['emails'] = __('Emails', 'invoicing'); |
|
| 377 | 377 | |
| 378 | - if ( count( getpaid_get_integration_settings() ) > 0 ) { |
|
| 379 | - $tabs['integrations'] = __( 'Integrations', 'invoicing' ); |
|
| 378 | + if (count(getpaid_get_integration_settings()) > 0) { |
|
| 379 | + $tabs['integrations'] = __('Integrations', 'invoicing'); |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - $tabs['privacy'] = __( 'Privacy', 'invoicing' ); |
|
| 383 | - $tabs['misc'] = __( 'Misc', 'invoicing' ); |
|
| 384 | - $tabs['tools'] = __( 'Tools', 'invoicing' ); |
|
| 382 | + $tabs['privacy'] = __('Privacy', 'invoicing'); |
|
| 383 | + $tabs['misc'] = __('Misc', 'invoicing'); |
|
| 384 | + $tabs['tools'] = __('Tools', 'invoicing'); |
|
| 385 | 385 | |
| 386 | - return apply_filters( 'wpinv_settings_tabs', $tabs ); |
|
| 386 | + return apply_filters('wpinv_settings_tabs', $tabs); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | -function wpinv_get_settings_tab_sections( $tab = false ) { |
|
| 389 | +function wpinv_get_settings_tab_sections($tab = false) { |
|
| 390 | 390 | $tabs = false; |
| 391 | 391 | $sections = wpinv_get_registered_settings_sections(); |
| 392 | 392 | |
| 393 | - if( $tab && ! empty( $sections[ $tab ] ) ) { |
|
| 394 | - $tabs = $sections[ $tab ]; |
|
| 393 | + if ($tab && !empty($sections[$tab])) { |
|
| 394 | + $tabs = $sections[$tab]; |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | return $tabs; |
@@ -400,91 +400,91 @@ discard block |
||
| 400 | 400 | function wpinv_get_registered_settings_sections() { |
| 401 | 401 | static $sections = false; |
| 402 | 402 | |
| 403 | - if ( false !== $sections ) { |
|
| 403 | + if (false !== $sections) { |
|
| 404 | 404 | return $sections; |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | $sections = array( |
| 408 | - 'general' => apply_filters( 'wpinv_settings_sections_general', array( |
|
| 409 | - 'main' => __( 'General Settings', 'invoicing' ), |
|
| 410 | - 'currency_section' => __( 'Currency Settings', 'invoicing' ), |
|
| 411 | - 'labels' => __( 'Label Texts', 'invoicing' ), |
|
| 412 | - ) ), |
|
| 413 | - 'gateways' => apply_filters( 'wpinv_settings_sections_gateways', array( |
|
| 414 | - 'main' => __( 'Gateway Settings', 'invoicing' ), |
|
| 415 | - ) ), |
|
| 416 | - 'taxes' => apply_filters( 'wpinv_settings_sections_taxes', array( |
|
| 417 | - 'main' => __( 'Tax Settings', 'invoicing' ), |
|
| 418 | - 'rates' => __( 'Tax Rates', 'invoicing' ), |
|
| 419 | - 'vat' => __( 'EU VAT Settings', 'invoicing' ) |
|
| 420 | - ) ), |
|
| 421 | - 'emails' => apply_filters( 'wpinv_settings_sections_emails', array( |
|
| 422 | - 'main' => __( 'Email Settings', 'invoicing' ), |
|
| 423 | - ) ), |
|
| 424 | - |
|
| 425 | - 'integrations' => wp_list_pluck( getpaid_get_integration_settings(), 'label', 'id' ), |
|
| 426 | - |
|
| 427 | - 'privacy' => apply_filters( 'wpinv_settings_sections_privacy', array( |
|
| 428 | - 'main' => __( 'Privacy policy', 'invoicing' ), |
|
| 429 | - ) ), |
|
| 430 | - 'misc' => apply_filters( 'wpinv_settings_sections_misc', array( |
|
| 431 | - 'main' => __( 'Miscellaneous', 'invoicing' ), |
|
| 432 | - 'custom-css' => __( 'Custom CSS', 'invoicing' ), |
|
| 433 | - ) ), |
|
| 434 | - 'tools' => apply_filters( 'wpinv_settings_sections_tools', array( |
|
| 435 | - 'main' => __( 'Diagnostic Tools', 'invoicing' ), |
|
| 436 | - ) ), |
|
| 408 | + 'general' => apply_filters('wpinv_settings_sections_general', array( |
|
| 409 | + 'main' => __('General Settings', 'invoicing'), |
|
| 410 | + 'currency_section' => __('Currency Settings', 'invoicing'), |
|
| 411 | + 'labels' => __('Label Texts', 'invoicing'), |
|
| 412 | + )), |
|
| 413 | + 'gateways' => apply_filters('wpinv_settings_sections_gateways', array( |
|
| 414 | + 'main' => __('Gateway Settings', 'invoicing'), |
|
| 415 | + )), |
|
| 416 | + 'taxes' => apply_filters('wpinv_settings_sections_taxes', array( |
|
| 417 | + 'main' => __('Tax Settings', 'invoicing'), |
|
| 418 | + 'rates' => __('Tax Rates', 'invoicing'), |
|
| 419 | + 'vat' => __('EU VAT Settings', 'invoicing') |
|
| 420 | + )), |
|
| 421 | + 'emails' => apply_filters('wpinv_settings_sections_emails', array( |
|
| 422 | + 'main' => __('Email Settings', 'invoicing'), |
|
| 423 | + )), |
|
| 424 | + |
|
| 425 | + 'integrations' => wp_list_pluck(getpaid_get_integration_settings(), 'label', 'id'), |
|
| 426 | + |
|
| 427 | + 'privacy' => apply_filters('wpinv_settings_sections_privacy', array( |
|
| 428 | + 'main' => __('Privacy policy', 'invoicing'), |
|
| 429 | + )), |
|
| 430 | + 'misc' => apply_filters('wpinv_settings_sections_misc', array( |
|
| 431 | + 'main' => __('Miscellaneous', 'invoicing'), |
|
| 432 | + 'custom-css' => __('Custom CSS', 'invoicing'), |
|
| 433 | + )), |
|
| 434 | + 'tools' => apply_filters('wpinv_settings_sections_tools', array( |
|
| 435 | + 'main' => __('Diagnostic Tools', 'invoicing'), |
|
| 436 | + )), |
|
| 437 | 437 | ); |
| 438 | 438 | |
| 439 | - $sections = apply_filters( 'wpinv_settings_sections', $sections ); |
|
| 439 | + $sections = apply_filters('wpinv_settings_sections', $sections); |
|
| 440 | 440 | |
| 441 | 441 | return $sections; |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | -function wpinv_get_pages( $with_slug = false, $default_label = NULL ) { |
|
| 444 | +function wpinv_get_pages($with_slug = false, $default_label = NULL) { |
|
| 445 | 445 | $pages_options = array(); |
| 446 | 446 | |
| 447 | - if( $default_label !== NULL && $default_label !== false ) { |
|
| 448 | - $pages_options = array( '' => $default_label ); // Blank option |
|
| 447 | + if ($default_label !== NULL && $default_label !== false) { |
|
| 448 | + $pages_options = array('' => $default_label); // Blank option |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | $pages = get_pages(); |
| 452 | - if ( $pages ) { |
|
| 453 | - foreach ( $pages as $page ) { |
|
| 452 | + if ($pages) { |
|
| 453 | + foreach ($pages as $page) { |
|
| 454 | 454 | $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
| 455 | - $pages_options[ $page->ID ] = $title; |
|
| 455 | + $pages_options[$page->ID] = $title; |
|
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | return $pages_options; |
| 460 | 460 | } |
| 461 | 461 | |
| 462 | -function wpinv_header_callback( $args ) { |
|
| 463 | - if ( !empty( $args['desc'] ) ) { |
|
| 462 | +function wpinv_header_callback($args) { |
|
| 463 | + if (!empty($args['desc'])) { |
|
| 464 | 464 | echo $args['desc']; |
| 465 | 465 | } |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | -function wpinv_hidden_callback( $args ) { |
|
| 468 | +function wpinv_hidden_callback($args) { |
|
| 469 | 469 | global $wpinv_options; |
| 470 | 470 | |
| 471 | - if ( isset( $args['set_value'] ) ) { |
|
| 471 | + if (isset($args['set_value'])) { |
|
| 472 | 472 | $value = $args['set_value']; |
| 473 | - } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 474 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 473 | + } elseif (isset($wpinv_options[$args['id']])) { |
|
| 474 | + $value = $wpinv_options[$args['id']]; |
|
| 475 | 475 | } else { |
| 476 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 476 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 477 | 477 | } |
| 478 | 478 | |
| 479 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
| 479 | + if (isset($args['faux']) && true === $args['faux']) { |
|
| 480 | 480 | $args['readonly'] = true; |
| 481 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 481 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 482 | 482 | $name = ''; |
| 483 | 483 | } else { |
| 484 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
| 484 | + $name = 'name="wpinv_settings[' . esc_attr($args['id']) . ']"'; |
|
| 485 | 485 | } |
| 486 | 486 | |
| 487 | - $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
| 487 | + $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key($args['id']) . ']" ' . $name . ' value="' . esc_attr(stripslashes($value)) . '" />'; |
|
| 488 | 488 | |
| 489 | 489 | echo $html; |
| 490 | 490 | } |
@@ -492,61 +492,61 @@ discard block |
||
| 492 | 492 | /** |
| 493 | 493 | * Displays a checkbox settings callback. |
| 494 | 494 | */ |
| 495 | -function wpinv_checkbox_callback( $args ) { |
|
| 495 | +function wpinv_checkbox_callback($args) { |
|
| 496 | 496 | |
| 497 | - $std = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 498 | - $std = wpinv_get_option( $args['id'], $std ); |
|
| 499 | - $id = esc_attr( $args['id'] ); |
|
| 497 | + $std = isset($args['std']) ? $args['std'] : ''; |
|
| 498 | + $std = wpinv_get_option($args['id'], $std); |
|
| 499 | + $id = esc_attr($args['id']); |
|
| 500 | 500 | |
| 501 | - getpaid_hidden_field( "wpinv_settings[$id]", '0' ); |
|
| 501 | + getpaid_hidden_field("wpinv_settings[$id]", '0'); |
|
| 502 | 502 | ?> |
| 503 | 503 | <fieldset> |
| 504 | 504 | <label> |
| 505 | - <input id="wpinv-settings-<?php echo $id; ?>" name="wpinv_settings[<?php echo $id; ?>]" <?php checked( empty( $std ), false ); ?> value="1" type="checkbox"> |
|
| 506 | - <?php echo wp_kses_post( $args['desc'] ); ?> |
|
| 505 | + <input id="wpinv-settings-<?php echo $id; ?>" name="wpinv_settings[<?php echo $id; ?>]" <?php checked(empty($std), false); ?> value="1" type="checkbox"> |
|
| 506 | + <?php echo wp_kses_post($args['desc']); ?> |
|
| 507 | 507 | </label> |
| 508 | 508 | </fieldset> |
| 509 | 509 | <?php |
| 510 | 510 | } |
| 511 | 511 | |
| 512 | -function wpinv_multicheck_callback( $args ) { |
|
| 512 | +function wpinv_multicheck_callback($args) { |
|
| 513 | 513 | |
| 514 | 514 | global $wpinv_options; |
| 515 | 515 | |
| 516 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 517 | - $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 516 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 517 | + $class = !empty($args['class']) ? ' ' . esc_attr($args['class']) : ''; |
|
| 518 | 518 | |
| 519 | - if ( ! empty( $args['options'] ) ) { |
|
| 519 | + if (!empty($args['options'])) { |
|
| 520 | 520 | |
| 521 | - $std = isset( $args['std'] ) ? $args['std'] : array(); |
|
| 522 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : $std; |
|
| 521 | + $std = isset($args['std']) ? $args['std'] : array(); |
|
| 522 | + $value = isset($wpinv_options[$args['id']]) ? $wpinv_options[$args['id']] : $std; |
|
| 523 | 523 | |
| 524 | 524 | echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
| 525 | - foreach( $args['options'] as $key => $option ): |
|
| 526 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 527 | - if ( in_array( $sanitize_key, $value ) ) { |
|
| 525 | + foreach ($args['options'] as $key => $option): |
|
| 526 | + $sanitize_key = wpinv_sanitize_key($key); |
|
| 527 | + if (in_array($sanitize_key, $value)) { |
|
| 528 | 528 | $enabled = $sanitize_key; |
| 529 | 529 | } else { |
| 530 | 530 | $enabled = NULL; |
| 531 | 531 | } |
| 532 | - echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
| 533 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
| 532 | + echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr($sanitize_key) . '" ' . checked($sanitize_key, $enabled, false) . '/> '; |
|
| 533 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post($option) . '</label></div>'; |
|
| 534 | 534 | endforeach; |
| 535 | 535 | echo '</div>'; |
| 536 | 536 | echo '<p class="description">' . $args['desc'] . '</p>'; |
| 537 | 537 | } |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | -function wpinv_payment_icons_callback( $args ) { |
|
| 540 | +function wpinv_payment_icons_callback($args) { |
|
| 541 | 541 | global $wpinv_options; |
| 542 | 542 | |
| 543 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 543 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 544 | 544 | |
| 545 | - if ( ! empty( $args['options'] ) ) { |
|
| 546 | - foreach( $args['options'] as $key => $option ) { |
|
| 547 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 545 | + if (!empty($args['options'])) { |
|
| 546 | + foreach ($args['options'] as $key => $option) { |
|
| 547 | + $sanitize_key = wpinv_sanitize_key($key); |
|
| 548 | 548 | |
| 549 | - if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
| 549 | + if (isset($wpinv_options[$args['id']][$key])) { |
|
| 550 | 550 | $enabled = $option; |
| 551 | 551 | } else { |
| 552 | 552 | $enabled = NULL; |
@@ -554,109 +554,109 @@ discard block |
||
| 554 | 554 | |
| 555 | 555 | echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
| 556 | 556 | |
| 557 | - echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
| 557 | + echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr($option) . '" ' . checked($option, $enabled, false) . '/> '; |
|
| 558 | 558 | |
| 559 | - if ( wpinv_string_is_image_url( $key ) ) { |
|
| 560 | - echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 559 | + if (wpinv_string_is_image_url($key)) { |
|
| 560 | + echo '<img class="payment-icon" src="' . esc_url($key) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 561 | 561 | } else { |
| 562 | - $card = strtolower( str_replace( ' ', '', $option ) ); |
|
| 562 | + $card = strtolower(str_replace(' ', '', $option)); |
|
| 563 | 563 | |
| 564 | - if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
| 565 | - $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
| 564 | + if (has_filter('wpinv_accepted_payment_' . $card . '_image')) { |
|
| 565 | + $image = apply_filters('wpinv_accepted_payment_' . $card . '_image', ''); |
|
| 566 | 566 | } else { |
| 567 | - $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
| 567 | + $image = wpinv_locate_template('images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false); |
|
| 568 | 568 | $content_dir = WP_CONTENT_DIR; |
| 569 | 569 | |
| 570 | - if ( function_exists( 'wp_normalize_path' ) ) { |
|
| 570 | + if (function_exists('wp_normalize_path')) { |
|
| 571 | 571 | // Replaces backslashes with forward slashes for Windows systems |
| 572 | - $image = wp_normalize_path( $image ); |
|
| 573 | - $content_dir = wp_normalize_path( $content_dir ); |
|
| 572 | + $image = wp_normalize_path($image); |
|
| 573 | + $content_dir = wp_normalize_path($content_dir); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | - $image = str_replace( $content_dir, content_url(), $image ); |
|
| 576 | + $image = str_replace($content_dir, content_url(), $image); |
|
| 577 | 577 | } |
| 578 | 578 | |
| 579 | - echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 579 | + echo '<img class="payment-icon" src="' . esc_url($image) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
| 580 | 580 | } |
| 581 | 581 | echo $option . '</label>'; |
| 582 | 582 | } |
| 583 | - echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
| 583 | + echo '<p class="description" style="margin-top:16px;">' . wp_kses_post($args['desc']) . '</p>'; |
|
| 584 | 584 | } |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
| 588 | 588 | * Displays a radio settings field. |
| 589 | 589 | */ |
| 590 | -function wpinv_radio_callback( $args ) { |
|
| 590 | +function wpinv_radio_callback($args) { |
|
| 591 | 591 | |
| 592 | - $std = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 593 | - $std = wpinv_get_option( $args['id'], $std ); |
|
| 592 | + $std = isset($args['std']) ? $args['std'] : ''; |
|
| 593 | + $std = wpinv_get_option($args['id'], $std); |
|
| 594 | 594 | ?> |
| 595 | 595 | <fieldset> |
| 596 | - <ul id="wpinv-settings-<?php echo esc_attr( $args['id'] ); ?>" style="margin-top: 0;"> |
|
| 597 | - <?php foreach( $args['options'] as $key => $option ) : ?> |
|
| 596 | + <ul id="wpinv-settings-<?php echo esc_attr($args['id']); ?>" style="margin-top: 0;"> |
|
| 597 | + <?php foreach ($args['options'] as $key => $option) : ?> |
|
| 598 | 598 | <li> |
| 599 | 599 | <label> |
| 600 | - <input name="wpinv_settings[<?php echo esc_attr( $args['id'] ); ?>]" <?php checked( $std, $key ); ?> value="<?php echo esc_attr( $key ); ?>" type="radio"> |
|
| 601 | - <?php echo wp_kses_post( $option ); ?> |
|
| 600 | + <input name="wpinv_settings[<?php echo esc_attr($args['id']); ?>]" <?php checked($std, $key); ?> value="<?php echo esc_attr($key); ?>" type="radio"> |
|
| 601 | + <?php echo wp_kses_post($option); ?> |
|
| 602 | 602 | </label> |
| 603 | 603 | </li> |
| 604 | 604 | <?php endforeach; ?> |
| 605 | 605 | </ul> |
| 606 | 606 | </fieldset> |
| 607 | 607 | <?php |
| 608 | - getpaid_settings_description_callback( $args ); |
|
| 608 | + getpaid_settings_description_callback($args); |
|
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | /** |
| 612 | 612 | * Displays a description if available. |
| 613 | 613 | */ |
| 614 | -function getpaid_settings_description_callback( $args ) { |
|
| 614 | +function getpaid_settings_description_callback($args) { |
|
| 615 | 615 | |
| 616 | - if ( ! empty( $args['desc'] ) ) { |
|
| 617 | - $description = wp_kses_post( $args['desc'] ); |
|
| 616 | + if (!empty($args['desc'])) { |
|
| 617 | + $description = wp_kses_post($args['desc']); |
|
| 618 | 618 | echo "<p class='description'>$description</p>"; |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | } |
| 622 | 622 | |
| 623 | -function wpinv_gateways_callback( $args ) { |
|
| 623 | +function wpinv_gateways_callback($args) { |
|
| 624 | 624 | global $wpinv_options; |
| 625 | 625 | |
| 626 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 626 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 627 | 627 | |
| 628 | - foreach ( $args['options'] as $key => $option ) : |
|
| 629 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
| 628 | + foreach ($args['options'] as $key => $option) : |
|
| 629 | + $sanitize_key = wpinv_sanitize_key($key); |
|
| 630 | 630 | |
| 631 | - if ( isset( $wpinv_options['gateways'][ $key ] ) ) |
|
| 631 | + if (isset($wpinv_options['gateways'][$key])) |
|
| 632 | 632 | $enabled = '1'; |
| 633 | 633 | else |
| 634 | 634 | $enabled = null; |
| 635 | 635 | |
| 636 | - echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 637 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
| 636 | + echo '<input name="wpinv_settings[' . esc_attr($args['id']) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
| 637 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html($option['admin_label']) . '</label><br/>'; |
|
| 638 | 638 | endforeach; |
| 639 | 639 | } |
| 640 | 640 | |
| 641 | 641 | function wpinv_gateway_select_callback($args) { |
| 642 | 642 | global $wpinv_options; |
| 643 | 643 | |
| 644 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 645 | - $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
| 644 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 645 | + $class = !empty($args['class']) ? ' ' . esc_attr($args['class']) : ''; |
|
| 646 | 646 | |
| 647 | - echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
| 647 | + echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="' . $class . '" >'; |
|
| 648 | 648 | |
| 649 | - foreach ( $args['options'] as $key => $option ) : |
|
| 650 | - if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
| 651 | - $selected = selected( $key, $args['selected'], false ); |
|
| 649 | + foreach ($args['options'] as $key => $option) : |
|
| 650 | + if (isset($args['selected']) && $args['selected'] !== null && $args['selected'] !== false) { |
|
| 651 | + $selected = selected($key, $args['selected'], false); |
|
| 652 | 652 | } else { |
| 653 | - $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : ''; |
|
| 653 | + $selected = isset($wpinv_options[$args['id']]) ? selected($key, $wpinv_options[$args['id']], false) : ''; |
|
| 654 | 654 | } |
| 655 | - echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
| 655 | + echo '<option value="' . wpinv_sanitize_key($key) . '"' . $selected . '>' . esc_html($option['admin_label']) . '</option>'; |
|
| 656 | 656 | endforeach; |
| 657 | 657 | |
| 658 | 658 | echo '</select>'; |
| 659 | - echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 659 | + echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | /** |
@@ -665,28 +665,28 @@ discard block |
||
| 665 | 665 | * @param array $args |
| 666 | 666 | * @return string |
| 667 | 667 | */ |
| 668 | -function wpinv_settings_attrs_helper( $args ) { |
|
| 668 | +function wpinv_settings_attrs_helper($args) { |
|
| 669 | 669 | |
| 670 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 671 | - $id = esc_attr( $args['id'] ); |
|
| 672 | - $placeholder = esc_attr( $args['placeholder'] ); |
|
| 670 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 671 | + $id = esc_attr($args['id']); |
|
| 672 | + $placeholder = esc_attr($args['placeholder']); |
|
| 673 | 673 | |
| 674 | - if ( ! empty( $args['faux'] ) ) { |
|
| 674 | + if (!empty($args['faux'])) { |
|
| 675 | 675 | $args['readonly'] = true; |
| 676 | 676 | $name = ''; |
| 677 | 677 | } else { |
| 678 | - $value = wpinv_get_option( $args['id'], $value ); |
|
| 678 | + $value = wpinv_get_option($args['id'], $value); |
|
| 679 | 679 | $name = "wpinv_settings[$id]"; |
| 680 | 680 | } |
| 681 | 681 | |
| 682 | - $value = is_scalar( $value ) ? esc_attr( $value ) : ''; |
|
| 683 | - $class = esc_attr( $args['class'] ); |
|
| 684 | - $style = esc_attr( $args['style'] ); |
|
| 685 | - $readonly = empty( $args['readonly'] ) ? '' : 'readonly onclick="this.select()"'; |
|
| 682 | + $value = is_scalar($value) ? esc_attr($value) : ''; |
|
| 683 | + $class = esc_attr($args['class']); |
|
| 684 | + $style = esc_attr($args['style']); |
|
| 685 | + $readonly = empty($args['readonly']) ? '' : 'readonly onclick="this.select()"'; |
|
| 686 | 686 | |
| 687 | 687 | $onchange = ''; |
| 688 | - if ( ! empty( $args['onchange'] ) ) { |
|
| 689 | - $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"'; |
|
| 688 | + if (!empty($args['onchange'])) { |
|
| 689 | + $onchange = ' onchange="' . esc_attr($args['onchange']) . '"'; |
|
| 690 | 690 | } |
| 691 | 691 | |
| 692 | 692 | return "name='$name' id='wpinv-settings-$id' style='$style' value='$value' class='$class' placeholder='$placeholder' data-placeholder='$placeholder' $onchange $readonly"; |
@@ -695,11 +695,11 @@ discard block |
||
| 695 | 695 | /** |
| 696 | 696 | * Displays a text input settings callback. |
| 697 | 697 | */ |
| 698 | -function wpinv_text_callback( $args ) { |
|
| 698 | +function wpinv_text_callback($args) { |
|
| 699 | 699 | |
| 700 | - $desc = wp_kses_post( $args['desc'] ); |
|
| 701 | - $desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>"; |
|
| 702 | - $attr = wpinv_settings_attrs_helper( $args ); |
|
| 700 | + $desc = wp_kses_post($args['desc']); |
|
| 701 | + $desc = empty($desc) ? '' : "<p class='description'>$desc</p>"; |
|
| 702 | + $attr = wpinv_settings_attrs_helper($args); |
|
| 703 | 703 | |
| 704 | 704 | ?> |
| 705 | 705 | <label style="width: 100%;"> |
@@ -713,14 +713,14 @@ discard block |
||
| 713 | 713 | /** |
| 714 | 714 | * Displays a number input settings callback. |
| 715 | 715 | */ |
| 716 | -function wpinv_number_callback( $args ) { |
|
| 716 | +function wpinv_number_callback($args) { |
|
| 717 | 717 | |
| 718 | - $desc = wp_kses_post( $args['desc'] ); |
|
| 719 | - $desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>"; |
|
| 720 | - $attr = wpinv_settings_attrs_helper( $args ); |
|
| 721 | - $max = intval( $args['max'] ); |
|
| 722 | - $min = intval( $args['min'] ); |
|
| 723 | - $step = floatval( $args['step'] ); |
|
| 718 | + $desc = wp_kses_post($args['desc']); |
|
| 719 | + $desc = empty($desc) ? '' : "<p class='description'>$desc</p>"; |
|
| 720 | + $attr = wpinv_settings_attrs_helper($args); |
|
| 721 | + $max = intval($args['max']); |
|
| 722 | + $min = intval($args['min']); |
|
| 723 | + $step = floatval($args['step']); |
|
| 724 | 724 | |
| 725 | 725 | ?> |
| 726 | 726 | <label style="width: 100%;"> |
@@ -731,47 +731,47 @@ discard block |
||
| 731 | 731 | |
| 732 | 732 | } |
| 733 | 733 | |
| 734 | -function wpinv_textarea_callback( $args ) { |
|
| 734 | +function wpinv_textarea_callback($args) { |
|
| 735 | 735 | global $wpinv_options; |
| 736 | 736 | |
| 737 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 737 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 738 | 738 | |
| 739 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 740 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 739 | + if (isset($wpinv_options[$args['id']])) { |
|
| 740 | + $value = $wpinv_options[$args['id']]; |
|
| 741 | 741 | } else { |
| 742 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 742 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 743 | 743 | } |
| 744 | 744 | |
| 745 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 746 | - $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text'; |
|
| 745 | + $size = (isset($args['size']) && !is_null($args['size'])) ? $args['size'] : 'regular'; |
|
| 746 | + $class = (isset($args['class']) && !is_null($args['class'])) ? $args['class'] : 'large-text'; |
|
| 747 | 747 | |
| 748 | - $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 749 | - $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 748 | + $html = '<textarea class="' . sanitize_html_class($class) . ' txtarea-' . sanitize_html_class($size) . ' wpi-' . esc_attr(sanitize_html_class($sanitize_id)) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']">' . esc_textarea(stripslashes($value)) . '</textarea>'; |
|
| 749 | + $html .= '<br /><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 750 | 750 | |
| 751 | 751 | echo $html; |
| 752 | 752 | } |
| 753 | 753 | |
| 754 | -function wpinv_password_callback( $args ) { |
|
| 754 | +function wpinv_password_callback($args) { |
|
| 755 | 755 | global $wpinv_options; |
| 756 | 756 | |
| 757 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 757 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 758 | 758 | |
| 759 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 760 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 759 | + if (isset($wpinv_options[$args['id']])) { |
|
| 760 | + $value = $wpinv_options[$args['id']]; |
|
| 761 | 761 | } else { |
| 762 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 762 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 763 | 763 | } |
| 764 | 764 | |
| 765 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 766 | - $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
| 767 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 765 | + $size = (isset($args['size']) && !is_null($args['size'])) ? $args['size'] : 'regular'; |
|
| 766 | + $html = '<input type="password" class="' . sanitize_html_class($size) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']" value="' . esc_attr($value) . '"/>'; |
|
| 767 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 768 | 768 | |
| 769 | 769 | echo $html; |
| 770 | 770 | } |
| 771 | 771 | |
| 772 | 772 | function wpinv_missing_callback($args) { |
| 773 | 773 | printf( |
| 774 | - __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
| 774 | + __('The callback function used for the %s setting is missing.', 'invoicing'), |
|
| 775 | 775 | '<strong>' . $args['id'] . '</strong>' |
| 776 | 776 | ); |
| 777 | 777 | } |
@@ -779,20 +779,20 @@ discard block |
||
| 779 | 779 | /** |
| 780 | 780 | * Displays a number input settings callback. |
| 781 | 781 | */ |
| 782 | -function wpinv_select_callback( $args ) { |
|
| 782 | +function wpinv_select_callback($args) { |
|
| 783 | 783 | |
| 784 | - $desc = wp_kses_post( $args['desc'] ); |
|
| 785 | - $desc = empty( $desc ) ? '' : "<p class='description'>$desc</p>"; |
|
| 786 | - $attr = wpinv_settings_attrs_helper( $args ); |
|
| 787 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 788 | - $value = wpinv_get_option( $args['id'], $value ); |
|
| 784 | + $desc = wp_kses_post($args['desc']); |
|
| 785 | + $desc = empty($desc) ? '' : "<p class='description'>$desc</p>"; |
|
| 786 | + $attr = wpinv_settings_attrs_helper($args); |
|
| 787 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 788 | + $value = wpinv_get_option($args['id'], $value); |
|
| 789 | 789 | |
| 790 | 790 | ?> |
| 791 | 791 | <label style="width: 100%;"> |
| 792 | 792 | <select <?php echo $attr; ?>> |
| 793 | - <?php foreach ( $args['options'] as $option => $name ) : ?> |
|
| 794 | - <option value="<?php echo esc_attr( $option ); ?>" <?php echo selected( is_array( $value ) ? in_array( "$option", $value, true ) : "$option" === $value ); ?>><?php echo wpinv_clean( $name ); ?></option> |
|
| 795 | - <?php endforeach;?> |
|
| 793 | + <?php foreach ($args['options'] as $option => $name) : ?> |
|
| 794 | + <option value="<?php echo esc_attr($option); ?>" <?php echo selected(is_array($value) ? in_array("$option", $value, true) : "$option" === $value); ?>><?php echo wpinv_clean($name); ?></option> |
|
| 795 | + <?php endforeach; ?> |
|
| 796 | 796 | </select> |
| 797 | 797 | <?php echo $desc; ?> |
| 798 | 798 | </label> |
@@ -800,95 +800,95 @@ discard block |
||
| 800 | 800 | |
| 801 | 801 | } |
| 802 | 802 | |
| 803 | -function wpinv_color_select_callback( $args ) { |
|
| 803 | +function wpinv_color_select_callback($args) { |
|
| 804 | 804 | global $wpinv_options; |
| 805 | 805 | |
| 806 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 806 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 807 | 807 | |
| 808 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 809 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 808 | + if (isset($wpinv_options[$args['id']])) { |
|
| 809 | + $value = $wpinv_options[$args['id']]; |
|
| 810 | 810 | } else { |
| 811 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 811 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 812 | 812 | } |
| 813 | 813 | |
| 814 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
| 814 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']"/>'; |
|
| 815 | 815 | |
| 816 | - foreach ( $args['options'] as $option => $color ) { |
|
| 817 | - $selected = selected( $option, $value, false ); |
|
| 818 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
| 816 | + foreach ($args['options'] as $option => $color) { |
|
| 817 | + $selected = selected($option, $value, false); |
|
| 818 | + $html .= '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html($color['label']) . '</option>'; |
|
| 819 | 819 | } |
| 820 | 820 | |
| 821 | 821 | $html .= '</select>'; |
| 822 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 822 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 823 | 823 | |
| 824 | 824 | echo $html; |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | -function wpinv_rich_editor_callback( $args ) { |
|
| 827 | +function wpinv_rich_editor_callback($args) { |
|
| 828 | 828 | global $wpinv_options, $wp_version; |
| 829 | 829 | |
| 830 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 830 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 831 | 831 | |
| 832 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 833 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 832 | + if (isset($wpinv_options[$args['id']])) { |
|
| 833 | + $value = $wpinv_options[$args['id']]; |
|
| 834 | 834 | |
| 835 | - if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
| 836 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 835 | + if (empty($args['allow_blank']) && empty($value)) { |
|
| 836 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 837 | 837 | } |
| 838 | 838 | } else { |
| 839 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 839 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | - $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
| 842 | + $rows = isset($args['size']) ? $args['size'] : 20; |
|
| 843 | 843 | |
| 844 | 844 | $html = '<div class="getpaid-settings-editor-input">'; |
| 845 | - if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
| 845 | + if ($wp_version >= 3.3 && function_exists('wp_editor')) { |
|
| 846 | 846 | ob_start(); |
| 847 | - wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
| 847 | + wp_editor(stripslashes($value), 'wpinv_settings_' . esc_attr($args['id']), array('textarea_name' => 'wpinv_settings[' . esc_attr($args['id']) . ']', 'textarea_rows' => absint($rows), 'media_buttons' => false)); |
|
| 848 | 848 | $html .= ob_get_clean(); |
| 849 | 849 | } else { |
| 850 | - $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
| 850 | + $html .= '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']" class="wpi-' . esc_attr(sanitize_html_class($args['id'])) . '">' . esc_textarea(stripslashes($value)) . '</textarea>'; |
|
| 851 | 851 | } |
| 852 | 852 | |
| 853 | - $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 853 | + $html .= '</div><br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 854 | 854 | |
| 855 | 855 | echo $html; |
| 856 | 856 | } |
| 857 | 857 | |
| 858 | -function wpinv_upload_callback( $args ) { |
|
| 858 | +function wpinv_upload_callback($args) { |
|
| 859 | 859 | global $wpinv_options; |
| 860 | 860 | |
| 861 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 861 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 862 | 862 | |
| 863 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 863 | + if (isset($wpinv_options[$args['id']])) { |
|
| 864 | 864 | $value = $wpinv_options[$args['id']]; |
| 865 | 865 | } else { |
| 866 | 866 | $value = isset($args['std']) ? $args['std'] : ''; |
| 867 | 867 | } |
| 868 | 868 | |
| 869 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
| 870 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
| 871 | - $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
| 872 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 869 | + $size = (isset($args['size']) && !is_null($args['size'])) ? $args['size'] : 'regular'; |
|
| 870 | + $html = '<input type="text" class="' . sanitize_html_class($size) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']" value="' . esc_attr(stripslashes($value)) . '"/>'; |
|
| 871 | + $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __('Upload File', 'invoicing') . '"/></span>'; |
|
| 872 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 873 | 873 | |
| 874 | 874 | echo $html; |
| 875 | 875 | } |
| 876 | 876 | |
| 877 | -function wpinv_color_callback( $args ) { |
|
| 877 | +function wpinv_color_callback($args) { |
|
| 878 | 878 | global $wpinv_options; |
| 879 | 879 | |
| 880 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 880 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 881 | 881 | |
| 882 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
| 883 | - $value = $wpinv_options[ $args['id'] ]; |
|
| 882 | + if (isset($wpinv_options[$args['id']])) { |
|
| 883 | + $value = $wpinv_options[$args['id']]; |
|
| 884 | 884 | } else { |
| 885 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 885 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
| 886 | 886 | } |
| 887 | 887 | |
| 888 | - $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
| 888 | + $default = isset($args['std']) ? $args['std'] : ''; |
|
| 889 | 889 | |
| 890 | - $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
| 891 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 890 | + $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']" value="' . esc_attr($value) . '" data-default-color="' . esc_attr($default) . '" />'; |
|
| 891 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 892 | 892 | |
| 893 | 893 | echo $html; |
| 894 | 894 | } |
@@ -896,9 +896,9 @@ discard block |
||
| 896 | 896 | function wpinv_country_states_callback($args) { |
| 897 | 897 | global $wpinv_options; |
| 898 | 898 | |
| 899 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
| 899 | + $sanitize_id = wpinv_sanitize_key($args['id']); |
|
| 900 | 900 | |
| 901 | - if ( isset( $args['placeholder'] ) ) { |
|
| 901 | + if (isset($args['placeholder'])) { |
|
| 902 | 902 | $placeholder = $args['placeholder']; |
| 903 | 903 | } else { |
| 904 | 904 | $placeholder = ''; |
@@ -906,16 +906,16 @@ discard block |
||
| 906 | 906 | |
| 907 | 907 | $states = wpinv_get_country_states(); |
| 908 | 908 | |
| 909 | - $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 910 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
| 909 | + $class = empty($states) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
| 910 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr($args['id']) . ']"' . $class . 'data-placeholder="' . esc_html($placeholder) . '"/>'; |
|
| 911 | 911 | |
| 912 | - foreach ( $states as $option => $name ) { |
|
| 913 | - $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
| 914 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
| 912 | + foreach ($states as $option => $name) { |
|
| 913 | + $selected = isset($wpinv_options[$args['id']]) ? selected($option, $wpinv_options[$args['id']], false) : ''; |
|
| 914 | + $html .= '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html($name) . '</option>'; |
|
| 915 | 915 | } |
| 916 | 916 | |
| 917 | 917 | $html .= '</select>'; |
| 918 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
| 918 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post($args['desc']) . '</label>'; |
|
| 919 | 919 | |
| 920 | 920 | echo $html; |
| 921 | 921 | } |
@@ -930,7 +930,7 @@ discard block |
||
| 930 | 930 | </tr> |
| 931 | 931 | <tr class="bsui"> |
| 932 | 932 | <td colspan="2" class="p-0"> |
| 933 | - <?php include plugin_dir_path( __FILE__ ) . 'views/html-tax-rates-edit.php'; ?> |
|
| 933 | + <?php include plugin_dir_path(__FILE__) . 'views/html-tax-rates-edit.php'; ?> |
|
| 934 | 934 | |
| 935 | 935 | <?php |
| 936 | 936 | |
@@ -939,14 +939,14 @@ discard block |
||
| 939 | 939 | /** |
| 940 | 940 | * Displays a tax rate' edit row. |
| 941 | 941 | */ |
| 942 | -function wpinv_tax_rate_callback( $tax_rate, $key, $echo = true ) { |
|
| 942 | +function wpinv_tax_rate_callback($tax_rate, $key, $echo = true) { |
|
| 943 | 943 | ob_start(); |
| 944 | 944 | |
| 945 | - $key = sanitize_key( $key ); |
|
| 946 | - $tax_rate['reduced_rate'] = empty( $tax_rate['reduced_rate'] ) ? 0 : $tax_rate['reduced_rate']; |
|
| 947 | - include plugin_dir_path( __FILE__ ) . 'views/html-tax-rate-edit.php'; |
|
| 945 | + $key = sanitize_key($key); |
|
| 946 | + $tax_rate['reduced_rate'] = empty($tax_rate['reduced_rate']) ? 0 : $tax_rate['reduced_rate']; |
|
| 947 | + include plugin_dir_path(__FILE__) . 'views/html-tax-rate-edit.php'; |
|
| 948 | 948 | |
| 949 | - if ( $echo ) { |
|
| 949 | + if ($echo) { |
|
| 950 | 950 | echo ob_get_clean(); |
| 951 | 951 | } else { |
| 952 | 952 | return ob_get_clean(); |
@@ -958,81 +958,81 @@ discard block |
||
| 958 | 958 | ob_start(); ?> |
| 959 | 959 | </td><tr> |
| 960 | 960 | <td colspan="2" class="wpinv_tools_tdbox"> |
| 961 | - <?php if ( $args['desc'] ) { ?><p><?php echo $args['desc']; ?></p><?php } ?> |
|
| 962 | - <?php do_action( 'wpinv_tools_before' ); ?> |
|
| 961 | + <?php if ($args['desc']) { ?><p><?php echo $args['desc']; ?></p><?php } ?> |
|
| 962 | + <?php do_action('wpinv_tools_before'); ?> |
|
| 963 | 963 | <table id="wpinv_tools_table" class="wp-list-table widefat fixed posts"> |
| 964 | 964 | <thead> |
| 965 | 965 | <tr> |
| 966 | - <th scope="col" class="wpinv-th-tool"><?php _e( 'Tool', 'invoicing' ); ?></th> |
|
| 967 | - <th scope="col" class="wpinv-th-desc"><?php _e( 'Description', 'invoicing' ); ?></th> |
|
| 968 | - <th scope="col" class="wpinv-th-action"><?php _e( 'Action', 'invoicing' ); ?></th> |
|
| 966 | + <th scope="col" class="wpinv-th-tool"><?php _e('Tool', 'invoicing'); ?></th> |
|
| 967 | + <th scope="col" class="wpinv-th-desc"><?php _e('Description', 'invoicing'); ?></th> |
|
| 968 | + <th scope="col" class="wpinv-th-action"><?php _e('Action', 'invoicing'); ?></th> |
|
| 969 | 969 | </tr> |
| 970 | 970 | </thead> |
| 971 | - <?php do_action( 'wpinv_tools_row' ); ?> |
|
| 971 | + <?php do_action('wpinv_tools_row'); ?> |
|
| 972 | 972 | <tbody> |
| 973 | 973 | </tbody> |
| 974 | 974 | </table> |
| 975 | - <?php do_action( 'wpinv_tools_after' ); ?> |
|
| 975 | + <?php do_action('wpinv_tools_after'); ?> |
|
| 976 | 976 | <?php |
| 977 | 977 | echo ob_get_clean(); |
| 978 | 978 | } |
| 979 | 979 | |
| 980 | -function wpinv_descriptive_text_callback( $args ) { |
|
| 981 | - echo wp_kses_post( $args['desc'] ); |
|
| 980 | +function wpinv_descriptive_text_callback($args) { |
|
| 981 | + echo wp_kses_post($args['desc']); |
|
| 982 | 982 | } |
| 983 | 983 | |
| 984 | -function wpinv_raw_html_callback( $args ) { |
|
| 984 | +function wpinv_raw_html_callback($args) { |
|
| 985 | 985 | echo $args['desc']; |
| 986 | 986 | } |
| 987 | 987 | |
| 988 | -function wpinv_hook_callback( $args ) { |
|
| 989 | - do_action( 'wpinv_' . $args['id'], $args ); |
|
| 988 | +function wpinv_hook_callback($args) { |
|
| 989 | + do_action('wpinv_' . $args['id'], $args); |
|
| 990 | 990 | } |
| 991 | 991 | |
| 992 | 992 | function wpinv_set_settings_cap() { |
| 993 | 993 | return wpinv_get_capability(); |
| 994 | 994 | } |
| 995 | -add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' ); |
|
| 995 | +add_filter('option_page_capability_wpinv_settings', 'wpinv_set_settings_cap'); |
|
| 996 | 996 | |
| 997 | -function wpinv_settings_sanitize_input( $value, $key ) { |
|
| 997 | +function wpinv_settings_sanitize_input($value, $key) { |
|
| 998 | 998 | |
| 999 | - if ( $key == 'tax_rate' ) { |
|
| 1000 | - $value = wpinv_sanitize_amount( $value ); |
|
| 999 | + if ($key == 'tax_rate') { |
|
| 1000 | + $value = wpinv_sanitize_amount($value); |
|
| 1001 | 1001 | $value = $value >= 100 ? 99 : $value; |
| 1002 | 1002 | } |
| 1003 | 1003 | |
| 1004 | 1004 | return $value; |
| 1005 | 1005 | } |
| 1006 | -add_filter( 'wpinv_settings_sanitize', 'wpinv_settings_sanitize_input', 10, 2 ); |
|
| 1006 | +add_filter('wpinv_settings_sanitize', 'wpinv_settings_sanitize_input', 10, 2); |
|
| 1007 | 1007 | |
| 1008 | -function wpinv_on_update_settings( $old_value, $value, $option ) { |
|
| 1009 | - $old = !empty( $old_value['remove_data_on_unistall'] ) ? 1 : ''; |
|
| 1010 | - $new = !empty( $value['remove_data_on_unistall'] ) ? 1 : ''; |
|
| 1008 | +function wpinv_on_update_settings($old_value, $value, $option) { |
|
| 1009 | + $old = !empty($old_value['remove_data_on_unistall']) ? 1 : ''; |
|
| 1010 | + $new = !empty($value['remove_data_on_unistall']) ? 1 : ''; |
|
| 1011 | 1011 | |
| 1012 | - if ( $old != $new ) { |
|
| 1013 | - update_option( 'wpinv_remove_data_on_invoice_unistall', $new ); |
|
| 1012 | + if ($old != $new) { |
|
| 1013 | + update_option('wpinv_remove_data_on_invoice_unistall', $new); |
|
| 1014 | 1014 | } |
| 1015 | 1015 | } |
| 1016 | -add_action( 'update_option_wpinv_settings', 'wpinv_on_update_settings', 10, 3 ); |
|
| 1017 | -add_action( 'wpinv_settings_tab_bottom_emails_new_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1018 | -add_action( 'wpinv_settings_tab_bottom_emails_cancelled_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1019 | -add_action( 'wpinv_settings_tab_bottom_emails_failed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1020 | -add_action( 'wpinv_settings_tab_bottom_emails_onhold_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1021 | -add_action( 'wpinv_settings_tab_bottom_emails_processing_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1022 | -add_action( 'wpinv_settings_tab_bottom_emails_completed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1023 | -add_action( 'wpinv_settings_tab_bottom_emails_refunded_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1024 | -add_action( 'wpinv_settings_tab_bottom_emails_user_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1025 | -add_action( 'wpinv_settings_tab_bottom_emails_user_note', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1026 | -add_action( 'wpinv_settings_tab_bottom_emails_overdue', 'wpinv_settings_tab_bottom_emails', 10, 2 ); |
|
| 1027 | - |
|
| 1028 | -function wpinv_settings_tab_bottom_emails( $active_tab, $section ) { |
|
| 1016 | +add_action('update_option_wpinv_settings', 'wpinv_on_update_settings', 10, 3); |
|
| 1017 | +add_action('wpinv_settings_tab_bottom_emails_new_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1018 | +add_action('wpinv_settings_tab_bottom_emails_cancelled_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1019 | +add_action('wpinv_settings_tab_bottom_emails_failed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1020 | +add_action('wpinv_settings_tab_bottom_emails_onhold_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1021 | +add_action('wpinv_settings_tab_bottom_emails_processing_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1022 | +add_action('wpinv_settings_tab_bottom_emails_completed_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1023 | +add_action('wpinv_settings_tab_bottom_emails_refunded_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1024 | +add_action('wpinv_settings_tab_bottom_emails_user_invoice', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1025 | +add_action('wpinv_settings_tab_bottom_emails_user_note', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1026 | +add_action('wpinv_settings_tab_bottom_emails_overdue', 'wpinv_settings_tab_bottom_emails', 10, 2); |
|
| 1027 | + |
|
| 1028 | +function wpinv_settings_tab_bottom_emails($active_tab, $section) { |
|
| 1029 | 1029 | ?> |
| 1030 | 1030 | <div class="wpinv-email-wc-row "> |
| 1031 | 1031 | <div class="wpinv-email-wc-td"> |
| 1032 | - <h3 class="wpinv-email-wc-title"><?php echo apply_filters( 'wpinv_settings_email_wildcards_title', __( 'Wildcards For Emails', 'invoicing' ) ); ?></h3> |
|
| 1032 | + <h3 class="wpinv-email-wc-title"><?php echo apply_filters('wpinv_settings_email_wildcards_title', __('Wildcards For Emails', 'invoicing')); ?></h3> |
|
| 1033 | 1033 | <p class="wpinv-email-wc-description"> |
| 1034 | 1034 | <?php |
| 1035 | - $description = __( 'The following wildcards can be used in email subjects, heading and content:<br> |
|
| 1035 | + $description = __('The following wildcards can be used in email subjects, heading and content:<br> |
|
| 1036 | 1036 | <strong>{site_title} :</strong> Site Title<br> |
| 1037 | 1037 | <strong>{name} :</strong> Customer\'s full name<br> |
| 1038 | 1038 | <strong>{first_name} :</strong> Customer\'s first name<br> |
@@ -1046,7 +1046,7 @@ discard block |
||
| 1046 | 1046 | <strong>{invoice_due_date} :</strong> The date the invoice is due<br> |
| 1047 | 1047 | <strong>{date} :</strong> Today\'s date.<br> |
| 1048 | 1048 | <strong>{is_was} :</strong> If due date of invoice is past, displays "was" otherwise displays "is"<br> |
| 1049 | - <strong>{invoice_label} :</strong> Invoices/quotes singular name. Ex: Invoice/Quote<br>', 'invoicing' ); |
|
| 1049 | + <strong>{invoice_label} :</strong> Invoices/quotes singular name. Ex: Invoice/Quote<br>', 'invoicing'); |
|
| 1050 | 1050 | echo apply_filters('wpinv_settings_email_wildcards_description', $description, $active_tab, $section); |
| 1051 | 1051 | ?> |
| 1052 | 1052 | </p> |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | * @since 1.0.19 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; |
|
| 9 | +defined('ABSPATH') || exit; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * The main API class |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | $this->settings = new GetPaid_REST_Settings_Controller(); |
| 57 | 57 | |
| 58 | 58 | // Fires after loading the rest api. |
| 59 | - do_action( 'getpaid_rest_api_loaded', $this ); |
|
| 59 | + do_action('getpaid_rest_api_loaded', $this); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | } |