forked from newsapps/wordpress-mtv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-plugin.php
196 lines (165 loc) · 6.6 KB
/
wp-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @package MTV
* @version 1.0
*/
/*
Plugin Name: Wordpress MTV
Plugin URI: http://blog.apps.chicagotribune.com
Description: A simple framework for building custom apps and features
on top of wordpress
Author: Ryan Mark, Ryan Nagle
Version: 1.0
*/
include 'mtv.php';
use mtv\shortcuts;
/**
* Initialize the MTV framework
**/
# MTV comes with an App for WordPressy stuff
mtv\register_app( 'wp',
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp' );
/**
* Register javascript libraries
**/
$js_runtime_settings = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'current_blog_id' => get_current_blog_id(),
'DEBUG' => false
);
if ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) {
wp_register_script('mtv-all',
plugins_url('/mtv/devjs/mtv.js'),
array('jquery'),
MTV_VERSION);
$js_runtime_settings['DEBUG'] = true;
} else {
wp_register_script('mtv-all',
plugins_url('/mtv/mtv.min.js'),
array('jquery'),
MTV_VERSION);
}
wp_localize_script('mtv-all', 'WordPress', $js_runtime_settings);
unset($js_runtime_settings);
/**
* Use the URL resolver for ajax calls
**/
$handle_ajax = function() {
// get the url patterns for the current theme
if ( file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
include get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php';
else if ( file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
include get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php';
else if ( ! empty( $GLOBALS['ajax_url_patterns'] ) )
global $ajax_url_patterns;
else
throw new Exception("Can't find a urls.php file in your theme");
// Since we're doing ajax, we've already loaded $registered_apps in
// our init callback and only need to resolve the url
\mtv\http\urlresolver(array(
'url' => get_default( $_REQUEST, 'path', ''),
'url_patterns' => $ajax_url_patterns));
// That's all folks
exit;
};
add_action('wp_ajax_mtv', $handle_ajax);
add_action('wp_ajax_nopriv_mtv', $handle_ajax);
/**
* Request handling
**/
add_filter( 'query_vars', function( $vars ) {
// Where we bless query vars so WP doesn't throw them out
$vars[] = 'path';
return $vars;
} );
add_action( 'init', function() {
load_plugin_textdomain('mtv', false, basename(__DIR__) . '/locale/');
/**
* Is our chosen theme an MTV theme?
* If not, we don't want to hijack rewrite rules and template selection
**/
if ( ! file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) &&
! file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
return; // nope
/**
* *_rewrite_rules
* Extra "permastruct" rules are run through this filter. Extra "permastruct" rules are added
* to $wp_rewrite->extra_rules_top and include rewrite rules for tags, categories, and post_formats.
* This only happens while generate_rewrite_rules runs. $wp_rewrite->extra_rules_top looks like
* it's typically used for 3rd party stuff, just not always.
*
* Anyway, it messes up our generate_rewrite_rules hook, so we have to prevent that stuff from
* getting added to $wp_rewrite->extra_rules_top
**/
global $wp_rewrite;
foreach ( array_keys( $wp_rewrite->extra_permastructs ) as $permastructname )
add_filter( $permastructname . '_rewrite_rules', function() { return array(); } );
/**
* generate_rewrite_rules
* Run immediately after WordPress generates it's rewrite rules. We'll replace all
* the rules with one of ours. Our rule will route all requests into our url resolver.
* We set this to run first so plugins can still add their own rules.
*
* P.S. $wp_rewrite is a object, so gets passed in by reference
**/
add_action( 'generate_rewrite_rules', function( $wp_rewrite ) {
# setup our hijack rules
$mtv_rules = array();
$mtv_rules['$'] = 'index.php?path'; // Fix WP 3.3 home rewrite rule
$mtv_rules['(.*)'] = 'index.php?path=$matches[1]';
# We're feeling adventurous, override wordpress' processing with ours
# If we just relace $wp_rewrite->rules, we lose stuff added by other plugins
# we just want to replace WordPress's default builtin stuff
$wp_rewrite->rules = array_merge($wp_rewrite->extra_rules_top, $mtv_rules, $wp_rewrite->extra_rules);
}, 1, 1);
/**
* redirect_canonical
* Correct opinionated wordpress redirects
**/
add_filter('redirect_canonical', function($redirect_url, $requested_url) {
# Don't add trailing slashes to files.
# if $redirect_url ends in '/' then
if ( substr($redirect_url, -1) ) {
$ext = pathinfo($requested_url,PATHINFO_EXTENSION);
# if $requested_url ends in '.xml' or '.html' etc. then
if ( in_array($ext, array('xml', 'html')) )
return false;
}
}, 10, 2);
/**
* Reroute the rest of the application through our url resolver
**/
add_action( 'template_redirect', function() {
// Where we figure out which view to use on the front end
global $wp_query;
// check for the path queryvar. That means we're on!
if ( !($wp_query->query_vars['path'] === NULL) ) { // will work for the root path
// reset wp_query's is_whatever flags and posts
shortcuts\reset_wp_query();
// get the url patterns for the current theme
if ( file_exists( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
include get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'urls.php';
else if ( file_exists( get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php' ) )
include get_template_directory() . DIRECTORY_SEPARATOR . 'urls.php';
else
throw new Exception("Can't find a urls.php file in your theme");
// whatever is in the $apps global is what we're going to load
global $apps;
// run MTV
mtv\run( array(
'url' => $wp_query->query_vars['path'],
'url_patterns' => $url_patterns,
'apps' => $apps ) );
// That's all folks
exit;
}
});
/**
* If we're doing ajax, load MTV here in the init callback so
* that it will be available throughout the request
**/
if ( is_admin() && defined('DOING_AJAX') && DOING_AJAX == true ) {
if ( !empty( $GLOBALS['apps'] ) )
mtv\load( $GLOBALS['apps'] );
}
}, 999);