redirection.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | use SearchRegex\Search_Source; |
| 4 | use SearchRegex\Result; |
| 5 | |
| 6 | class Redirection_Search_Regex extends Search_Source { |
| 7 | public function get_actions( Result $result ) { |
| 8 | $edit = admin_url( 'tools.php?page=redirection.php' ); |
| 9 | |
| 10 | return [ |
| 11 | 'edit' => $edit, |
| 12 | ]; |
| 13 | } |
| 14 | |
| 15 | public function get_columns() { |
| 16 | $columns = [ |
| 17 | 'url', |
| 18 | 'action_data', |
| 19 | 'title', |
| 20 | ]; |
| 21 | |
| 22 | return $columns; |
| 23 | } |
| 24 | |
| 25 | public function get_column_label( $column ) { |
| 26 | $labels = [ |
| 27 | 'url' => __( 'URL', 'search-regex' ), |
| 28 | 'title' => __( 'Title', 'search-regex' ), |
| 29 | ]; |
| 30 | |
| 31 | if ( isset( $labels[ $column ] ) ) { |
| 32 | return $labels[ $column ]; |
| 33 | } |
| 34 | |
| 35 | return $column; |
| 36 | } |
| 37 | |
| 38 | public function get_table_id() { |
| 39 | return 'id'; |
| 40 | } |
| 41 | |
| 42 | public function get_table_name() { |
| 43 | global $wpdb; |
| 44 | |
| 45 | return $wpdb->prefix . 'redirection_items'; |
| 46 | } |
| 47 | |
| 48 | public function get_title_column() { |
| 49 | return 'url'; |
| 50 | } |
| 51 | |
| 52 | public function save( $row_id, $column_id, $content ) { |
| 53 | $item = \Red_Item::get_by_id( $row_id ); |
| 54 | |
| 55 | if ( ! is_wp_error( $item ) ) { |
| 56 | /** @psalm-suppress PossiblyUndefinedMethod */ |
| 57 | $json = $item->to_json(); |
| 58 | |
| 59 | if ( isset( $json[ $column_id ] ) ) { |
| 60 | $json[ $column_id ] = $content; |
| 61 | /** @psalm-suppress PossiblyUndefinedMethod */ |
| 62 | $saved = $item->update( $json ); |
| 63 | |
| 64 | if ( is_wp_error( $saved ) ) { |
| 65 | return $saved; |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return new WP_Error( 'searchregex', 'Failed to update redirection' ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | add_filter( 'searchregex_sources_plugin', function( $plugins ) { |
| 77 | // Only show if Redirection is loaded |
| 78 | if ( defined( 'REDIRECTION_VERSION' ) ) { |
| 79 | $plugins[] = [ |
| 80 | 'name' => 'redirection', |
| 81 | 'label' => __( 'Redirection', 'search-regex' ), |
| 82 | 'description' => __( 'Search your redirects', 'search-regex' ), |
| 83 | 'class' => 'Redirection_Search_Regex', |
| 84 | 'type' => 'plugin', |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | return $plugins; |
| 89 | } ); |
| 90 |