-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.wp-cribbage.php
50 lines (40 loc) · 1.47 KB
/
class.wp-cribbage.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
<?php
class WP_Cribbage {
function __construct() {
add_action( 'init', array( $this, 'create_main_route' ) );
add_action( 'init', array( $this, 'maybe_flush_rewrites' ), 999 );
add_action( 'template_include', array( $this, 'maybe_serve_template' ), 99 );
}
/**
* Creates rewrites based on our app's url base - yoursite.com/play-cribbage
*/
function create_main_route() {
add_rewrite_rule( '^app-route/?$','index.php?cribbage=/','top' );
add_rewrite_rule( '^app-route(.*)?','index.php?cribbage=$matches[1]','top' );
global $wp;
$wp->add_query_var( 'cribbage' );
}
/**
* Flush rewrites when plugin is first loaded, and whenever the version changes
* Note: `flush_rewrite_rules()` should be used sparingly
*/
function maybe_flush_rewrites() {
$version = get_option( 'wp-cribbage_version', null );
if ( empty( $version ) || $version !== WP_CRIBBAGE_VERSION ) {
flush_rewrite_rules();
update_option( 'wp_react_plugin_version', WP_CRIBBAGE_VERSION );
}
}
/**
* If we are within our app route, register any js and css, and serve our main template
*/
function maybe_serve_template( $template ) {
if ( empty( $GLOBALS['wp']->query_vars['cribbage'] ) )
return $template;
wp_register_script( 'wp_cribbage', WP_CRIBBAGE_PATH . 'client/build/bundle.min.js' );
wp_register_style( 'wp_cribbage', WP_CRIBBAGE_PATH . 'client/css/app.min.css' );
$wp_cribbage_template = WP_CRIBBAGE_DIR . '/app.php';
return $wp_cribbage_template;
}
}
new WP_Cribbage();