class-comment-meta.php
5 months ago
class-comment.php
5 months ago
class-meta.php
5 months ago
class-options.php
5 months ago
class-post-meta.php
5 months ago
class-post.php
5 months ago
class-terms.php
4 months ago
class-user-meta.php
5 months ago
class-user.php
5 months ago
class-options.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source\Core; |
| 4 | |
| 5 | use SearchRegex\Source; |
| 6 | use SearchRegex\Plugin; |
| 7 | |
| 8 | /** |
| 9 | * Source for WP options |
| 10 | */ |
| 11 | class Options extends Source\Source { |
| 12 | public function get_table_id() { |
| 13 | return 'option_id'; |
| 14 | } |
| 15 | |
| 16 | public function get_table_name() { |
| 17 | global $wpdb; |
| 18 | |
| 19 | return $wpdb->options; |
| 20 | } |
| 21 | |
| 22 | public function get_title_column() { |
| 23 | return 'option_name'; |
| 24 | } |
| 25 | |
| 26 | public function save( $row_id, array $changes ) { |
| 27 | global $wpdb; |
| 28 | |
| 29 | // Get current option name. The table name is a known sanitized value |
| 30 | // phpcs:ignore |
| 31 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_name,option_value,autoload FROM {$this->get_table_name()} WHERE option_id=%d", $row_id ) ); |
| 32 | if ( ! $row ) { |
| 33 | return new \WP_Error( 'searchregex', 'Unable to update option' ); |
| 34 | } |
| 35 | |
| 36 | $option = $this->get_columns_to_change( $changes ); |
| 37 | |
| 38 | if ( count( $option ) > 0 ) { |
| 39 | $this->log_save( 'option', $option ); |
| 40 | |
| 41 | // This does all the sanitization |
| 42 | if ( Plugin\Settings::init()->can_save() ) { |
| 43 | if ( isset( $option['option_name'] ) ) { |
| 44 | // Changing the option name. Delete the current option and then recreate with the new option. This ensures it is correctly sanitized |
| 45 | delete_option( $row->option_name ); |
| 46 | } |
| 47 | |
| 48 | // This handles all sanitization |
| 49 | if ( update_option( (string) $row->option_name, $option['option_value'] ?? '', isset( $option['autoload'] ) ? (bool) $option['autoload'] : null ) ) { |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return new \WP_Error( 'searchregex', 'Failed to update option.' ); |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | public function delete_row( $row_id ) { |
| 61 | global $wpdb; |
| 62 | |
| 63 | $this->log_save( 'delete option', $row_id ); |
| 64 | |
| 65 | if ( Plugin\Settings::init()->can_save() ) { |
| 66 | // Get the option name for the row. This is so we can use the WP delete_option and have the cache cleared |
| 67 | // phpcs:ignore |
| 68 | $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_name,option_value,autoload FROM {$this->get_table_name()} WHERE option_id=%d", $row_id ) ); |
| 69 | if ( $row ) { |
| 70 | if ( delete_option( $row->option_name ) ) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return new \WP_Error( 'searchregex_delete', 'Failed to delete option', 401 ); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | public function autocomplete( array $column, $value ) { |
| 82 | global $wpdb; |
| 83 | |
| 84 | if ( in_array( $column['column'], [ 'option_name', 'option_value' ], true ) ) { |
| 85 | // phpcs:ignore |
| 86 | return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT " . $column['column'] . " as id," . $column['column'] . " as value FROM {$wpdb->options} WHERE " . $column['column'] . " LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) ); |
| 87 | } |
| 88 | |
| 89 | return []; |
| 90 | } |
| 91 | |
| 92 | public function get_schema() { |
| 93 | global $wpdb; |
| 94 | |
| 95 | return [ |
| 96 | 'name' => __( 'Options', 'search-regex' ), |
| 97 | 'table' => $wpdb->options, |
| 98 | 'columns' => [ |
| 99 | [ |
| 100 | 'column' => 'option_id', |
| 101 | 'type' => 'integer', |
| 102 | 'title' => __( 'ID', 'search-regex' ), |
| 103 | 'modify' => false, |
| 104 | ], |
| 105 | [ |
| 106 | 'column' => 'option_name', |
| 107 | 'type' => 'string', |
| 108 | 'title' => __( 'Name', 'search-regex' ), |
| 109 | 'options' => 'api', |
| 110 | 'global' => true, |
| 111 | ], |
| 112 | [ |
| 113 | 'column' => 'option_value', |
| 114 | 'type' => 'string', |
| 115 | 'title' => __( 'Value', 'search-regex' ), |
| 116 | 'options' => 'api', |
| 117 | 'multiline' => true, |
| 118 | 'global' => true, |
| 119 | ], |
| 120 | [ |
| 121 | 'column' => 'autoload', |
| 122 | 'type' => 'member', |
| 123 | 'options' => [ |
| 124 | [ |
| 125 | 'value' => 'yes', |
| 126 | 'label' => __( 'Is autoload', 'search-regex' ), |
| 127 | ], |
| 128 | [ |
| 129 | 'value' => 'no', |
| 130 | 'label' => __( 'Is not autoload', 'search-regex' ), |
| 131 | ], |
| 132 | ], |
| 133 | 'title' => __( 'Autoload', 'search-regex' ), |
| 134 | 'multiple' => false, |
| 135 | ], |
| 136 | ], |
| 137 | ]; |
| 138 | } |
| 139 | } |
| 140 |