| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | Plugin Name: Test Rewrites |
|---|
| 5 | Plugin URI: http://simonwheatley.co.uk/wordpress/trw |
|---|
| 6 | Description: Demonstrate the problem with not knowing when to add_rewrite_rules. |
|---|
| 7 | Version: 0.2 |
|---|
| 8 | Author: Simon Wheatley |
|---|
| 9 | Author URI: http://simonwheatley.co.uk/wordpress/ |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /* Copyright 2011 Simon Wheatley |
|---|
| 13 | |
|---|
| 14 | This program is free software; you can redistribute it and/or modify |
|---|
| 15 | it under the terms of the GNU General Public License as published by |
|---|
| 16 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 17 | (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | This program is distributed in the hope that it will be useful, |
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | GNU General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | You should have received a copy of the GNU General Public License |
|---|
| 25 | along with this program; if not, write to the Free Software |
|---|
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 27 | |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Called by the delete_option_rewrite_rules to add rewrite rules |
|---|
| 32 | * before the flush rebuilds them. |
|---|
| 33 | * |
|---|
| 34 | * @return void |
|---|
| 35 | **/ |
|---|
| 36 | function sil_call_add_rewrite_rules() { |
|---|
| 37 | error_log( "SILMyPlugin: sil_call_add_rewrite_rules" ); |
|---|
| 38 | add_rewrite_rule( 'confirmation/([0-9]+)/([^/]+)/?$', 'index.php?sil_confirm=1&confirm_id=$matches[1]&confirm_key=$matches[2]', 'top' ); |
|---|
| 39 | // flush_rewrite_rules(); |
|---|
| 40 | global $wp_rewrite; |
|---|
| 41 | error_log( "Just added, rules:" . print_r( $wp_rewrite->wp_rewrite_rules(), true ) ); |
|---|
| 42 | } |
|---|
| 43 | add_action( 'init', 'sil_call_add_rewrite_rules' ); |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Hooked when the permalink options page loads. |
|---|
| 47 | * |
|---|
| 48 | * @return void |
|---|
| 49 | * @author Simon Wheatley |
|---|
| 50 | **/ |
|---|
| 51 | function sil_activate() { |
|---|
| 52 | error_log( "SILMyPlugin: Activation hook" ); |
|---|
| 53 | sil_call_add_rewrite_rules(); |
|---|
| 54 | flush_rewrite_rules(); |
|---|
| 55 | } |
|---|
| 56 | register_activation_hook( __FILE__, 'sil_activate' ); |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Designed to model the vagaries of a cruel universe, this function hooks |
|---|
| 60 | * the init action with a ridiculously early priority and unexpectedly |
|---|
| 61 | * flushes the rewrite rules to try and catch us out. It succeeds. |
|---|
| 62 | * |
|---|
| 63 | * @return void |
|---|
| 64 | **/ |
|---|
| 65 | function unexpected_flush() { |
|---|
| 66 | global $wp_rewrite; |
|---|
| 67 | error_log( "Early init, rules:" . print_r( $wp_rewrite->wp_rewrite_rules(), true ) ); |
|---|
| 68 | flush_rewrite_rules(); |
|---|
| 69 | error_log( "Post-unexpected flush, rules:" . print_r( $wp_rewrite->wp_rewrite_rules(), true ) ); |
|---|
| 70 | } |
|---|
| 71 | add_action( 'init', 'unexpected_flush', -999 ); |
|---|
| 72 | |
|---|
| 73 | ?> |
|---|