PluginProbe
Redirection / 2.7.1
Redirection v2.7.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-admin.php

redirection-admin.php in Redirection 2.7.1, at redirection-admin.php

247 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include dirname( __FILE__ ).'/models/group.php';
4 include dirname( __FILE__ ).'/models/monitor.php';
5 include dirname( __FILE__ ).'/models/file-io.php';
6 include dirname( __FILE__ ).'/redirection-api.php';
7
8 define( 'RED_DEFAULT_PER_PAGE', 25 );
9 define( 'RED_MAX_PER_PAGE', 200 );
10
11 class Redirection_Admin {
12 private static $instance = null;
13 private $monitor;
14
15 static function init() {
16 if ( is_null( self::$instance ) ) {
17 self::$instance = new Redirection_Admin();
18
19 load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' );
20 }
21
22 return self::$instance;
23 }
24
25 function __construct() {
26 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
27 add_action( 'load-tools_page_redirection', array( $this, 'redirection_head' ) );
28 add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( $this, 'plugin_settings' ), 10, 4 );
29 add_action( 'redirection_save_options', array( $this, 'flush_schedule' ) );
30 add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 );
31
32 if ( defined( 'REDIRECTION_FLYING_SOLO' ) && REDIRECTION_FLYING_SOLO ) {
33 add_filter( 'script_loader_src', array( $this, 'flying_solo' ), 10, 2 );
34 }
35
36 register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) );
37 register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) );
38
39 $this->monitor = new Red_Monitor( red_get_options() );
40 $this->api = new Redirection_Api();
41 }
42
43 public static function plugin_activated() {
44 Redirection_Admin::update();
45 Red_Flusher::schedule();
46
47 update_option( 'redirection_options', red_get_options() );
48 }
49
50 public static function plugin_deactivated() {
51 Red_Flusher::clear();
52 }
53
54 public static function plugin_uninstall() {
55 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
56
57 $db = new RE_Database();
58 $db->remove( REDIRECTION_FILE );
59
60 delete_option( 'redirection_options' );
61 }
62
63 // So it finally came to this... some plugins include their JS in all pages, whether they are needed or not. If there is an error
64 // then this can prevent Redirection running, it's a little sensitive about that. We use the nuclear option here to disable
65 // all other JS while viewing Redirection
66 public function flying_solo( $src, $handle ) {
67 if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'page=redirection.php' ) !== false ) {
68 if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) {
69 return false;
70 }
71 }
72
73 return $src;
74 }
75
76 public function flush_schedule() {
77 Red_Flusher::schedule();
78 }
79
80 private static function update() {
81 $version = get_option( 'redirection_version' );
82
83 Red_Flusher::schedule();
84
85 if ( $version !== REDIRECTION_DB_VERSION ) {
86 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
87
88 $database = new RE_Database();
89
90 if ( $version === false ) {
91 $database->install();
92 }
93
94 return $database->upgrade( $version, REDIRECTION_DB_VERSION );
95 }
96
97 return true;
98 }
99
100 function set_per_page( $status, $option, $value ) {
101 if ( $option === 'redirection_log_per_page' ) {
102 return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) );
103 }
104
105 return $status;
106 }
107
108 function plugin_settings( $links ) {
109 $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&amp;sub=options">'.__( 'Settings', 'redirection' ).'</a>';
110 array_unshift( $links, $settings_link );
111 return $links;
112 }
113
114 function redirection_head() {
115 global $wp_version;
116
117 $build = REDIRECTION_VERSION.'-'.REDIRECTION_BUILD;
118 $options = red_get_options();
119
120 $this->inject();
121
122 if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) {
123 add_screen_option( 'per_page', array( 'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), RED_MAX_PER_PAGE ), 'default' => RED_DEFAULT_PER_PAGE, 'option' => 'redirection_log_per_page' ) );
124 }
125
126 if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) {
127 wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true );
128 } else {
129 wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $build, true );
130 }
131
132 wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.css', array(), $build );
133
134 wp_localize_script( 'redirection', 'Redirectioni10n', array(
135 'WP_API_root' => admin_url( 'admin-ajax.php' ),
136 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
137 'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
138 'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ),
139 'per_page' => $this->get_per_page(),
140 'locale' => $this->get_i18n_data(),
141 'localeSlug' => get_locale(),
142 'token' => $options['token'],
143 'autoGenerate' => $options['auto_target'],
144 'versions' => implode( ', ', array( 'Plugin '.REDIRECTION_VERSION, 'WordPress '.$wp_version, 'PHP '.phpversion() ) ),
145 'version' => REDIRECTION_VERSION,
146 ) );
147 }
148
149 private function get_per_page() {
150 $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 );
151
152 return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE;
153 }
154
155 private function get_i18n_data() {
156 $i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json';
157
158 if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
159 $locale_data = @file_get_contents( $i18n_json );
160
161 if ( $locale_data ) {
162 return $locale_data;
163 }
164 }
165
166 // Return empty if we have nothing to return so it doesn't fail when parsed in JS
167 return '{}';
168 }
169
170 function admin_menu() {
171 add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) );
172 }
173
174 function admin_screen() {
175 Redirection_Admin::update();
176
177 $version = get_plugin_data( REDIRECTION_FILE );
178 $version = $version['Version'];
179 ?>
180 <div id="react-ui">
181 <div class="react-loading">
182 <h1><?php _e( 'Loading the bits, please wait...', 'redirection' ); ?></h1>
183
184 <span class="react-loading-spinner" />
185 </div>
186 <noscript>Please enable JavaScript</noscript>
187 </div>
188
189 <?php
190 }
191
192 private function user_has_access() {
193 return current_user_can( apply_filters( 'redirection_role', 'administrator' ) );
194 }
195
196 function inject() {
197 if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) {
198 $this->tryExportLogs();
199 $this->tryExportRedirects();
200 $this->tryExportRSS();
201 }
202 }
203
204 function tryExportRSS() {
205 if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) {
206 $options = red_get_options();
207
208 if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) {
209 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
210
211 $exporter = Red_FileIO::create( 'rss' );
212 $exporter->force_download();
213 echo $exporter->get_data( $items, array() );
214 die();
215 }
216 }
217 }
218
219 private function tryExportLogs() {
220 if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) {
221 if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) {
222 RE_Log::export_to_csv();
223 } else {
224 RE_404::export_to_csv();
225 }
226
227 die();
228 }
229 }
230
231 private function tryExportRedirects() {
232 if ( $this->user_has_access() && $_GET['sub'] === 'modules' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) {
233 $export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] );
234
235 if ( $export !== false ) {
236 $export['exporter']->force_download();
237 echo $export['data'];
238 die();
239 }
240 }
241 }
242 }
243
244 register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
245
246 add_action( 'init', array( 'Redirection_Admin', 'init' ) );
247