-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticfront.plugin.php
118 lines (100 loc) · 3.46 KB
/
staticfront.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
<?php
class StaticFront extends Plugin
{
public function action_plugin_activation( $file )
{
Options::set( 'staticfront__page', 'none' );
Options::set( 'staticfront__blog_index', 'blog' );
Options::set( 'staticfront__keep_pages', TRUE );
}
public function configure()
{
$ui= new FormUI( 'staticfront' );
$page= $ui->append( 'select', 'page', 'staticfront__page', _t('The page to show for the home page: ', 'staticfront') );
$page->options['none']= _t('Show Normal Posts', 'staticfront');
foreach( $this->get_all_pages() as $post ) {
$page->options[$post->slug]= $post->title;
}
$blog_index= $ui->append( 'text', 'blog_index', 'staticfront__blog_index', sprintf( _t('Show normal posts at this URL: <b>%s</b>', 'staticfront'), Site::get_url( 'habari', true ) ) );
$blog_index->add_validator( 'validate_required' );
$keep_pages= $ui->append( 'checkbox', 'keep_pages', 'staticfront__keep_pages', _t( 'Show static pages at base url: ', 'staticfront' ) );
$ui->append( 'submit', 'save', _t('Save') );
$ui->success( array( $this, 'updated_config' ) );
return $ui;
}
public function updated_config( $ui )
{
Session::notice( 'StaticFront options updated!' );
$ui->save();
}
private function get_all_pages()
{
$pages= Posts::get( array( 'content_type' => Post::type('page'), 'nolimit' => 1 ) );
return $pages;
}
public function filter_theme_act_display_home( $handled, $theme )
{
$page= Options::get( 'staticfront__page' );
if ( $page && $page != 'none' ) {
$post= Post::get( array( 'slug' => $page ) );
$theme->act_display( array( 'posts' => $post ) );
return TRUE;
}
return FALSE;
}
public function filter_theme_act_display_blog_home( $handled, $theme )
{
$page= Options::get( 'staticfront__page' );
if ( $page && $page != 'none' ) {
$paramarray['fallback']= array(
'{$type}.multiple',
'multiple',
);
// Makes sure home displays only entries
$paramarray['user_filters']= array(
'content_type' => Post::type('entry'),
);
$theme->act_display( $paramarray );
return TRUE;
}
return FALSE;
}
public function filter_rewrite_rules( $rules )
{
if ( Options::get( 'staticfront__page' ) != 'none' ) {
$base= trim( Options::get( 'staticfront__blog_index' ) , '/' );
$stay= Options::get( 'staticfront__keep_pages' );
$move = array(
'display_entries_by_date' => $base . '/',
'display_entry' => $base . '/',
'display_entries' => $base . '/',
'display_entries_by_tag' => $base . '/',
'display_search' => $base . '/',
'display_page' => $base . '/',
'display_404' => $base . '/',
);
if( Options::get( 'staticfront__keep_pages' ) ) {
$move['display_page'] = '';
}
foreach( $rules as $rule ) {
if( isset( $move[$rule->name] ) && strpos( $rule->build_str, $base . '/' ) === FALSE && strpos( $rule->parse_regex, '/^' ) === FALSE ) {
$rule->parse_regex= substr( $rule->parse_regex, 0, 2 ) . $move[$rule->name] . substr( $rule->parse_regex, 2 );
$rule->build_str= $move[$rule->name] . $rule->build_str;
}
}
$rules[] = new RewriteRule(array(
'name' => 'display_blog_home',
'parse_regex' => '%^' . $base . '(?:/page/(?P<page>\d+))?/?$%',
'build_str' => '' . $base . '(/page/{$page})',
'handler' => 'UserThemeHandler',
'action' => 'display_blog_home',
'priority' => 1,
'rule_class' => RewriteRule::RULE_PLUGIN,
'is_active' => 1,
'description' => 'Blog index display for StaticFront' )
);
}
return $rules;
}
}
?>