PluginProbe
Redirection / 2.8.1
Redirection v2.8.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.8.1, at redirection-admin.php

319 lines 10.4 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 if ( $this->ignore_this_plugin( $src ) ) {
70 return false;
71 }
72 }
73 }
74
75 return $src;
76 }
77
78 private function ignore_this_plugin( $src ) {
79 if ( strpos( $src, 'mootools' ) !== false ) {
80 return true;
81 }
82
83 if ( strpos( $src, 'wp-seo-' ) !== false ) {
84 return true;
85 }
86
87 return false;
88 }
89
90 public function flush_schedule() {
91 Red_Flusher::schedule();
92 }
93
94 private static function update() {
95 $version = get_option( 'redirection_version' );
96
97 Red_Flusher::schedule();
98
99 if ( $version !== REDIRECTION_DB_VERSION ) {
100 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
101
102 $database = new RE_Database();
103
104 if ( $version === false ) {
105 $database->install();
106 }
107
108 return $database->upgrade( $version, REDIRECTION_DB_VERSION );
109 }
110
111 return true;
112 }
113
114 function set_per_page( $status, $option, $value ) {
115 if ( $option === 'redirection_log_per_page' ) {
116 return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) );
117 }
118
119 return $status;
120 }
121
122 function plugin_settings( $links ) {
123 $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&amp;sub=options">'.__( 'Settings', 'redirection' ).'</a>';
124 array_unshift( $links, $settings_link );
125 return $links;
126 }
127
128 function redirection_head() {
129 global $wp_version;
130
131 $build = REDIRECTION_VERSION.'-'.REDIRECTION_BUILD;
132 $options = red_get_options();
133 $versions = array(
134 'Plugin: '.REDIRECTION_VERSION,
135 'WordPress: '.$wp_version,
136 'PHP: '.phpversion(),
137 'Browser: '.Redirection_Request::get_user_agent(),
138 );
139
140 $this->inject();
141
142 if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) {
143 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' ) );
144 }
145
146 if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) {
147 wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true );
148 } else {
149 wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $build, true );
150 }
151
152 wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.css', array(), $build );
153
154 wp_localize_script( 'redirection', 'Redirectioni10n', array(
155 'WP_API_root' => admin_url( 'admin-ajax.php' ),
156 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
157 'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
158 'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ),
159 'per_page' => $this->get_per_page(),
160 'locale' => $this->get_i18n_data(),
161 'localeSlug' => get_locale(),
162 'token' => $options['token'],
163 'autoGenerate' => $options['auto_target'],
164 'versions' => implode( "\n", $versions ),
165 'version' => REDIRECTION_VERSION,
166 ) );
167 }
168
169 private function get_per_page() {
170 $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 );
171
172 return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE;
173 }
174
175 private function get_i18n_data() {
176 $i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json';
177
178 if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
179 $locale_data = @file_get_contents( $i18n_json );
180
181 if ( $locale_data ) {
182 return $locale_data;
183 }
184 }
185
186 // Return empty if we have nothing to return so it doesn't fail when parsed in JS
187 return '{}';
188 }
189
190 function admin_menu() {
191 add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) );
192 }
193
194 function admin_screen() {
195 Redirection_Admin::update();
196
197 $version = get_plugin_data( REDIRECTION_FILE );
198 $version = $version['Version'];
199 ?>
200 <div id="react-ui">
201 <div class="react-loading">
202 <h1><?php _e( 'Loading, please wait...', 'redirection' ); ?></h1>
203
204 <span class="react-loading-spinner"></span>
205 </div>
206 <noscript>Please enable JavaScript</noscript>
207
208 <div class="react-error" style="display: none">
209 <h1><?php _e( 'Unable to load Redirection', 'redirection' ); ?> v<?php echo esc_html( $version ); ?></h1>
210 <p><?php _e( "This may be caused by another plugin - look at your browser's error console for more details.", 'redirection' ); ?></p>
211 <p><?php _e( 'If you are using a page caching plugin or service (CloudFlare, OVH, etc) then you can also try clearing that cache.', 'redirection' ); ?></p>
212 <p><?php _e( 'Also check if your browser is able to load <code>redirection.js</code>:', 'redirection' ); ?></p>
213 <p><code><?php echo esc_html( plugin_dir_url( REDIRECTION_FILE ).'redirection.js?ver='.urlencode( REDIRECTION_VERSION ).'-'.urlencode( REDIRECTION_BUILD ) ); ?></code></p>
214 <p><?php _e( "If you think Redirection is at fault then create an issue.", 'redirection' ); ?></p>
215 <p class="versions"><?php _e( '<code>Redirectioni10n</code> is not defined. This usually means another plugin is blocking Redirection from loading. Please disable all plugins and try again.', 'redirection' ); ?></p>
216 <p>
217 <a class="button-primary" target="_blank" href="https://github.com/johngodley/redirection/issues/new?title=Problem%20starting%20Redirection%20<?php echo esc_attr( $version ) ?>">
218 <?php _e( 'Create Issue', 'redirection' ); ?>
219 </a>
220 </p>
221 </div>
222 </div>
223
224 <script>
225 var prevError = window.onerror;
226 var errors = [];
227 var timeout = 0;
228 var timer = setInterval( function() {
229 if ( isRedirectionLoaded() ) {
230 resetAll();
231 } else if ( errors.length > 0 || timeout++ === 5 ) {
232 showError();
233 }
234 }, 5000 );
235
236 function isRedirectionLoaded() {
237 return typeof redirection !== 'undefined';
238 }
239
240 function showError() {
241 resetAll();
242 document.querySelector( '.react-loading' ).style.display = 'none';
243 document.querySelector( '.react-error' ).style.display = 'block';
244
245 if ( typeof Redirectioni10n !== 'undefined' ) {
246 document.querySelector( '.versions' ).innerHTML = Redirectioni10n.versions.replace( /\n/g, '<br />' );
247 document.querySelector( '.react-error .button-primary' ).href += '&body=' + encodeURIComponent( "```\n" + errors.join( ',' ) + "\n```\n\n" ) + encodeURIComponent( Redirectioni10n.versions );
248 }
249 }
250
251 function resetAll() {
252 clearInterval( timer );
253 window.onerror = prevError;
254 }
255
256 window.onerror = function( error, url, line ) {
257 console.error( error );
258 errors.push( error + ' ' + url + ' ' + line );
259 };
260 </script>
261 <?php
262 }
263
264 private function user_has_access() {
265 return current_user_can( apply_filters( 'redirection_role', 'administrator' ) );
266 }
267
268 function inject() {
269 if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) {
270 $this->tryExportLogs();
271 $this->tryExportRedirects();
272 $this->tryExportRSS();
273 }
274 }
275
276 function tryExportRSS() {
277 if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) {
278 $options = red_get_options();
279
280 if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) {
281 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
282
283 $exporter = Red_FileIO::create( 'rss' );
284 $exporter->force_download();
285 echo $exporter->get_data( $items, array() );
286 die();
287 }
288 }
289 }
290
291 private function tryExportLogs() {
292 if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) {
293 if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) {
294 RE_Log::export_to_csv();
295 } else {
296 RE_404::export_to_csv();
297 }
298
299 die();
300 }
301 }
302
303 private function tryExportRedirects() {
304 if ( $this->user_has_access() && $_GET['sub'] === 'io' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) {
305 $export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] );
306
307 if ( $export !== false ) {
308 $export['exporter']->force_download();
309 echo $export['data'];
310 die();
311 }
312 }
313 }
314 }
315
316 register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
317
318 add_action( 'init', array( 'Redirection_Admin', 'init' ) );
319