comment-meta.php
6 years ago
comment.php
6 years ago
meta.php
6 years ago
options.php
6 years ago
post-meta.php
6 years ago
post.php
6 years ago
user-meta.php
6 years ago
user.php
6 years ago
options.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex; |
| 4 | |
| 5 | use SearchRegex\Search_Source; |
| 6 | |
| 7 | class Source_Options extends Search_Source { |
| 8 | public function get_columns() { |
| 9 | $columns = [ |
| 10 | 'option_name', |
| 11 | 'option_value', |
| 12 | ]; |
| 13 | |
| 14 | return $columns; |
| 15 | } |
| 16 | |
| 17 | public function get_column_label( $column ) { |
| 18 | $labels = [ |
| 19 | 'option_name' => __( 'Name', 'search-regex' ), |
| 20 | 'option_value' => __( 'Value', 'search-regex' ), |
| 21 | ]; |
| 22 | |
| 23 | if ( isset( $labels[ $column ] ) ) { |
| 24 | return $labels[ $column ]; |
| 25 | } |
| 26 | |
| 27 | return $column; |
| 28 | } |
| 29 | |
| 30 | public function get_table_id() { |
| 31 | return 'option_id'; |
| 32 | } |
| 33 | |
| 34 | public function get_table_name() { |
| 35 | global $wpdb; |
| 36 | |
| 37 | return $wpdb->options; |
| 38 | } |
| 39 | |
| 40 | public function get_title_column() { |
| 41 | return 'option_name'; |
| 42 | } |
| 43 | |
| 44 | public function save( $row_id, $column_id, $content ) { |
| 45 | global $wpdb; |
| 46 | |
| 47 | // Get current option name. The table name is a known sanitized value |
| 48 | // phpcs:ignore |
| 49 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_name,option_value,autoload FROM {$this->get_table_name()} WHERE option_id=%d", $row_id ) ); |
| 50 | if ( ! $row ) { |
| 51 | return new \WP_Error( 'searchregex', 'Unable to update option' ); |
| 52 | } |
| 53 | |
| 54 | if ( $column_id === 'option_name' ) { |
| 55 | // Changing the option name. Delete the current option and then recreate with the new option. This ensures it is correctly sanitized |
| 56 | delete_option( $row->option_name ); |
| 57 | |
| 58 | // Insert as a new option |
| 59 | if ( add_option( $content, $row->option_value, '', $row->autoload ) ) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | return new \WP_Error( 'searchregex', 'Unable to update option' ); |
| 64 | } |
| 65 | |
| 66 | // This handles all sanitization |
| 67 | if ( update_option( $row->option_name, $content ) ) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | return new \WP_Error( 'searchregex', 'Unable to update option' ); |
| 72 | } |
| 73 | |
| 74 | public function delete_row( $row_id ) { |
| 75 | global $wpdb; |
| 76 | |
| 77 | // Get current option name. The table name is a known sanitized value |
| 78 | // phpcs:ignore |
| 79 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_name,option_value,autoload FROM {$this->get_table_name()} WHERE option_id=%d", $row_id ) ); |
| 80 | if ( ! $row ) { |
| 81 | return new \WP_Error( 'searchregex', 'Failed to delete option' ); |
| 82 | } |
| 83 | |
| 84 | if ( delete_option( $row->option_name ) ) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | return new \WP_Error( 'searchregex_delete', 'Failed to delete option', 401 ); |
| 89 | } |
| 90 | } |
| 91 |