Make WordPress Core

Ticket #18450: demo.php

File demo.php, 2.3 KB (added by simonwheatley, 13 years ago)

Refactored to use the flush on activation method

Line 
1<?php
2
3/*
4Plugin Name: Test Rewrites
5Plugin URI: http://simonwheatley.co.uk/wordpress/trw
6Description: Demonstrate the problem with not knowing when to add_rewrite_rules.
7Version: 0.2
8Author: Simon Wheatley
9Author URI: http://simonwheatley.co.uk/wordpress/
10*/
11 
12/*  Copyright 2011 Simon Wheatley
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, 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 **/
36function 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}
43add_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 **/
51function sil_activate() {
52        error_log( "SILMyPlugin: Activation hook" );
53        sil_call_add_rewrite_rules();
54        flush_rewrite_rules();
55}
56register_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 **/
65function 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}
71add_action( 'init', 'unexpected_flush', -999 );
72
73?>